writing binary file (ios::binary)

Discussion in 'C++' started by Ron Eggler, Apr 25, 2008.

  1. Ron Eggler

    Ron Eggler Guest

    Hi,

    I would like to write binary data in a file i open (ofstream) with
    ios::binary but it keeps failing and it gives me a segmentation fault.
    What I'm exactly doing:
    [C++]
    if (isBinary == true)
    {
    std::cout << "open(" << filename.c_str() << ", std::ios::binary |
    std::ios::trunc);" << std::endl;
    ofs->open(filename.c_str(), std::ios::binary | std::ios::trunc);
    }
    [/C++]
    And the output on my shell(as root) looks like:
    [shell]
    Filename: /root/PRS_UPDATE/prs
    open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    Segmentation fault (core dumped)
    [/shell]
    The permissions of folder /root/PRS_UPDATE look like:
    root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    total 8
    drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..

    I'm not understanding, anyone an idea where the problem could be?
    Any suggestions are highly appreciated!

    Thank you!

    Ron
    --
    weeks of software enineering safe hours of planing ;)
     
    Ron Eggler, Apr 25, 2008
    #1
    1. Advertising

  2. On 2008-04-25 18:40, Ron Eggler wrote:
    > Hi,
    >
    > I would like to write binary data in a file i open (ofstream) with
    > ios::binary but it keeps failing and it gives me a segmentation fault.
    > What I'm exactly doing:
    > [C++]
    > if (isBinary == true)
    > {
    > std::cout << "open(" << filename.c_str() << ", std::ios::binary |
    > std::ios::trunc);" << std::endl;
    > ofs->open(filename.c_str(), std::ios::binary | std::ios::trunc);
    > }
    > [/C++]
    > And the output on my shell(as root) looks like:
    > [shell]
    > Filename: /root/PRS_UPDATE/prs
    > open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    > Segmentation fault (core dumped)
    > [/shell]
    > The permissions of folder /root/PRS_UPDATE look like:
    > root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    > total 8
    > drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    > drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..
    >
    > I'm not understanding, anyone an idea where the problem could be?
    > Any suggestions are highly appreciated!


    No idea, but compiling the program with debug symbols and starting it in
    gdb will help you figure it out.

    --
    Erik Wikström
     
    Erik Wikström, Apr 25, 2008
    #2
    1. Advertising

  3. Ron Eggler

    Christopher Guest

    On Apr 25, 11:40 am, Ron Eggler <> wrote:
    > Hi,
    >
    > I would like to write binary data in a file i open (ofstream) with
    > ios::binary but it keeps failing and it gives me a segmentation fault.
    > What I'm exactly doing:
    > [C++]
    > if (isBinary == true)
    > {
    > std::cout << "open(" << filename.c_str() << ", std::ios::binary |
    > std::ios::trunc);" << std::endl;
    > ofs->open(filename.c_str(), std::ios::binary | std::ios::trunc);
    > }
    > [/C++]
    > And the output on my shell(as root) looks like:
    > [shell]
    > Filename: /root/PRS_UPDATE/prs
    > open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    > Segmentation fault (core dumped)
    > [/shell]
    > The permissions of folder /root/PRS_UPDATE look like:
    > root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    > total 8
    > drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    > drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..
    >
    > I'm not understanding, anyone an idea where the problem could be?
    > Any suggestions are highly appreciated!
    >
    > Thank you!
    >
    > Ron
    > --
    > weeks of software enineering safe hours of planing ;)



    You didn't give enough information
    Where did the seg fault occur? what line?
    What were the values of the variables involved at that line?
    What are the variable types?

    I think you'll find that learning to use the debugger of your choice
    will save you hours of frustration.
     
    Christopher, Apr 25, 2008
    #3
  4. Ron Eggler

    Ron Eggler Guest

    Erik Wikström wrote:

    > On 2008-04-25 18:40, Ron Eggler wrote:
    >> Hi,
    >>
    >> I would like to write binary data in a file i open (ofstream) with
    >> ios::binary but it keeps failing and it gives me a segmentation fault.
    >> What I'm exactly doing:
    >> [C++]
    >> if (isBinary == true)
    >> {
    >> std::cout << "open(" << filename.c_str() << ",
    >> std::ios::binary |
    >> std::ios::trunc);" << std::endl;
    >> ofs->open(filename.c_str(), std::ios::binary |
    >> std::ios::trunc);
    >> }
    >> [/C++]
    >> And the output on my shell(as root) looks like:
    >> [shell]
    >> Filename: /root/PRS_UPDATE/prs
    >> open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    >> Segmentation fault (core dumped)
    >> [/shell]
    >> The permissions of folder /root/PRS_UPDATE look like:
    >> root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    >> total 8
    >> drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    >> drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..
    >>
    >> I'm not understanding, anyone an idea where the problem could be?
    >> Any suggestions are highly appreciated!

    >
    > No idea, but compiling the program with debug symbols and starting it in
    > gdb will help you figure it out.
    >


    yup, i've tried using gdb and this is what i got:
    [GDB]
    open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1211189568 (LWP 23001)]
    0xb7f36537 in std::__basic_file<char>::is_open ()
    from /usr/lib/libstdc++.so.6
    (gdb) bt
    #0 0xb7f36537 in std::__basic_file<char>::is_open ()
    from /usr/lib/libstdc++.so.6
    #1 0xb7edf2f6 in std::basic_ofstream<char, std::char_traits<char> >::eek:pen
    ()
    from /usr/lib/libstdc++.so.6
    #2 0x0804b189 in ?? ()
    #3 0x0804cae2 in ?? ()
    #4 0x0804a38d in ?? ()
    #5 0xb7d02050 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
    #6 0x080497a1 in ?? ()
    (gdb)
    [/GDB]
    This doesn't help me much, it doesn't really say anything but the line.
    Do have any other ideas? :eek:

    Thanks,
    Ron

    --
    weeks of software enineering safe hours of planing ;)
     
    Ron Eggler, Apr 25, 2008
    #4
  5. Ron Eggler

    Ron Eggler Guest

    Christopher wrote:

    > On Apr 25, 11:40 am, Ron Eggler <> wrote:
    >> Hi,
    >>
    >> I would like to write binary data in a file i open (ofstream) with
    >> ios::binary but it keeps failing and it gives me a segmentation fault.
    >> What I'm exactly doing:
    >> [C++]
    >> if (isBinary == true)
    >> {
    >> std::cout << "open(" << filename.c_str() << ",
    >> std::ios::binary |
    >> std::ios::trunc);" << std::endl;
    >> ofs->open(filename.c_str(), std::ios::binary |
    >> std::ios::trunc);
    >> }
    >> [/C++]
    >> And the output on my shell(as root) looks like:
    >> [shell]
    >> Filename: /root/PRS_UPDATE/prs
    >> open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    >> Segmentation fault (core dumped)
    >> [/shell]
    >> The permissions of folder /root/PRS_UPDATE look like:
    >> root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    >> total 8
    >> drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    >> drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..
    >>
    >> I'm not understanding, anyone an idea where the problem could be?
    >> Any suggestions are highly appreciated!
    >>
    >> Thank you!
    >>
    >> Ron
    >> --
    >> weeks of software enineering safe hours of planing ;)

    >
    >
    > You didn't give enough information
    > Where did the seg fault occur? what line?

    when i call ofs->open()
    > What were the values of the variables involved at that line?

    ofs, filename and namesace std
    > What are the variable types?

    ofstream, std::string, namespace
    >
    > I think you'll find that learning to use the debugger of your choice
    > will save you hours of frustration.

    Have tried using GDB:
    [GDB]
    open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1211189568 (LWP 23001)]
    0xb7f36537 in std::__basic_file<char>::is_open ()
    from /usr/lib/libstdc++.so.6
    (gdb) bt
    #0 0xb7f36537 in std::__basic_file<char>::is_open ()
    from /usr/lib/libstdc++.so.6
    #1 0xb7edf2f6 in std::basic_ofstream<char, std::char_traits<char> >::eek:pen
    ()
    from /usr/lib/libstdc++.so.6
    #2 0x0804b189 in ?? ()
    #3 0x0804cae2 in ?? ()
    #4 0x0804a38d in ?? ()
    #5 0xb7d02050 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
    #6 0x080497a1 in ?? ()
    (gdb)
    [/GDB]
    Any other ideas? :eek:i I'm pretty much stuck here :(
    Thanks,
    Ron

    --
    weeks of software enineering safe hours of planing ;)
     
    Ron Eggler, Apr 25, 2008
    #5
  6. Ron Eggler

    Christopher Guest

    On Apr 25, 3:08 pm, Ron Eggler <> wrote:
    > Christopher wrote:
    > > On Apr 25, 11:40 am, Ron Eggler <> wrote:

    {snip]
    > > You didn't give enough information
    > > Where did the seg fault occur? what line?

    >
    > when i call ofs->open()> What were the values of the variables involved at that line?
    >
    > ofs, filename and namesace std


    Those aren't values, those are 2 types and a namespace's name.
    Make sure they actually point to something, that the string contains
    something, etc. Step through and examine their contents.

    [Snip]
    > Have tried using GDB:
    > [GDB]
    > open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    >
    > Program received signal SIGSEGV, Segmentation fault.
    > [Switching to Thread -1211189568 (LWP 23001)]
    > 0xb7f36537 in std::__basic_file<char>::is_open ()
    > from /usr/lib/libstdc++.so.6
    > (gdb) bt
    > #0 0xb7f36537 in std::__basic_file<char>::is_open ()
    > from /usr/lib/libstdc++.so.6
    > #1 0xb7edf2f6 in std::basic_ofstream<char, std::char_traits<char> >::eek:pen
    > ()
    > from /usr/lib/libstdc++.so.6
    > #2 0x0804b189 in ?? ()
    > #3 0x0804cae2 in ?? ()
    > #4 0x0804a38d in ?? ()
    > #5 0xb7d02050 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
    > #6 0x080497a1 in ?? ()
    > (gdb)
    > [/GDB]



    I'm gonna make a guess:
    Since your using the ofstream via ofs->open, instead of myfile.open(),
    that ofs is a bad pointer. Meaning it is pointing to 0x0000 or that it
    is pointing to a location that is not an initialized ofstream.

    I wouldn't think it is some permission or other problem from not being
    able to open the file. It looks like a problem that occurs before the
    attempt to open the file occurs. i.e the call itself and its
    parameters. If a file can't be opened, the error bit is flipped in an
    fstream, it doesn't seg fault.
     
    Christopher, Apr 25, 2008
    #6
  7. Ron Eggler

    Ron Eggler Guest

    Thanks for lifiting me on this,

    the problem actually was that who ever started writing this code (I'm just
    here to get it working ;) ) declared ofs as an ofstream pointer. That's
    why ->, I just removed the pointer and replaced all -> with .and it works
    just fine. So fstream::eek:pen() doesn't work with an empty pointer, it
    actually needs a valid fstream handle to load the file handle in there -
    right?

    Did I catch the theoretics correcetly?

    Ron

    Christopher wrote:

    > On Apr 25, 3:08 pm, Ron Eggler <> wrote:
    >> Christopher wrote:
    >> > On Apr 25, 11:40 am, Ron Eggler <> wrote:

    > {snip]
    >> > You didn't give enough information
    >> > Where did the seg fault occur? what line?

    >>
    >> when i call ofs->open()> What were the values of the variables involved
    >> at that line?
    >>
    >> ofs, filename and namesace std

    >
    > Those aren't values, those are 2 types and a namespace's name.
    > Make sure they actually point to something, that the string contains
    > something, etc. Step through and examine their contents.
    >
    > [Snip]
    >> Have tried using GDB:
    >> [GDB]
    >> open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    >>
    >> Program received signal SIGSEGV, Segmentation fault.
    >> [Switching to Thread -1211189568 (LWP 23001)]
    >> 0xb7f36537 in std::__basic_file<char>::is_open ()
    >> from /usr/lib/libstdc++.so.6
    >> (gdb) bt
    >> #0 0xb7f36537 in std::__basic_file<char>::is_open ()
    >> from /usr/lib/libstdc++.so.6
    >> #1 0xb7edf2f6 in std::basic_ofstream<char, std::char_traits<char>
    >> #>::eek:pen
    >> ()
    >> from /usr/lib/libstdc++.so.6
    >> #2 0x0804b189 in ?? ()
    >> #3 0x0804cae2 in ?? ()
    >> #4 0x0804a38d in ?? ()
    >> #5 0xb7d02050 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
    >> #6 0x080497a1 in ?? ()
    >> (gdb)
    >> [/GDB]

    >
    >
    > I'm gonna make a guess:
    > Since your using the ofstream via ofs->open, instead of myfile.open(),
    > that ofs is a bad pointer. Meaning it is pointing to 0x0000 or that it
    > is pointing to a location that is not an initialized ofstream.
    >
    > I wouldn't think it is some permission or other problem from not being
    > able to open the file. It looks like a problem that occurs before the
    > attempt to open the file occurs. i.e the call itself and its
    > parameters. If a file can't be opened, the error bit is flipped in an
    > fstream, it doesn't seg fault.


    --
    weeks of software enineering safe hours of planing ;)
     
    Ron Eggler, Apr 26, 2008
    #7
  8. Ron Eggler

    Jim Langston Guest

    Ron Eggler wrote:
    > Thanks for lifiting me on this,
    >
    > the problem actually was that who ever started writing this code (I'm
    > just here to get it working ;) ) declared ofs as an ofstream pointer.
    > That's why ->, I just removed the pointer and replaced all -> with
    > .and it works just fine. So fstream::eek:pen() doesn't work with an
    > empty pointer, it actually needs a valid fstream handle to load the
    > file handle in there - right?
    >
    > Did I catch the theoretics correcetly?


    Pretty much, but it's a good idea when working with C and C++ to understand
    *why*. ofs is a pointer that's pointing to ... nothing. Some random spot
    in memory. It hasn't been initialized, memory hasn't been set aside for it,
    etc... Then when you acutally try to use it with ->open it looks at
    wherever it happens to be pointing in memory and thinking it's pointing to
    an initialzed object treats them as such. So basically your program is
    using random bytes of memory (whatever happens to be in whatever memory
    locatoin ofc happens to be pointing to) and tries to treat it as a file.
    All kind of bad things can happen which is undefined behavior, basically
    meaning anything can happen.

    It seems that somewhere along the line some random bit of memory was treated
    as if it was a memory pointer and was tried to be read or written to, maybe
    even the pointer itself, which is where the system error came in. Basically
    saying, hey, wait a second buddy, you don't own that memory! I ain't gonna
    let you touch it!

    Unitialized pointers are a BAD(tm) thing.
     
    Jim Langston, Apr 26, 2008
    #8
  9. Ron Eggler

    James Kanze Guest

    On Apr 25, 7:45 pm, Erik Wikström <> wrote:
    > On 2008-04-25 18:40, Ron Eggler wrote:
    > > I would like to write binary data in a file i open
    > > (ofstream) with ios::binary but it keeps failing and it
    > > gives me a segmentation fault. What I'm exactly doing:
    > > [C++]
    > > if (isBinary == true)
    > > {
    > > std::cout << "open(" << filename.c_str() << ", std::ios::binary |
    > > std::ios::trunc);" << std::endl;
    > > ofs->open(filename.c_str(), std::ios::binary | std::ios::trunc);
    > > }
    > > [/C++]
    > > And the output on my shell(as root) looks like:
    > > [shell]
    > > Filename: /root/PRS_UPDATE/prs
    > > open(/root/PRS_UPDATE/prs, std::ios::binary | std::ios::trunc);
    > > Segmentation fault (core dumped)
    > > [/shell]
    > > The permissions of folder /root/PRS_UPDATE look like:
    > > root@NovaxPRG-T1111:~/src/updater/build# ls -la /root/PRS_UPDATE/
    > > total 8
    > > drwxrwxrwx 2 reg users 4096 2008-04-25 09:10 .
    > > drwxr-xr-x 10 root root 4096 2008-04-25 09:10 ..


    > > I'm not understanding, anyone an idea where the problem
    > > could be? Any suggestions are highly appreciated!


    > No idea, but compiling the program with debug symbols and
    > starting it in gdb will help you figure it out.


    Before doing that, I'd ask what ofs points to. Until you've
    answered that question, there's no point in the debugger (and
    once you've answered it, you've probably already solved the
    problem).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 28, 2008
    #9
  10. Ron Eggler

    James Kanze Guest

    On Apr 26, 1:31 am, Ron Eggler <> wrote:
    > the problem actually was that who ever started writing this
    > code (I'm just here to get it working ;) ) declared ofs as an
    > ofstream pointer. That's why ->, I just removed the pointer
    > and replaced all -> with .and it works just fine. So
    > fstream::eek:pen() doesn't work with an empty pointer, it
    > actually needs a valid fstream handle to load the file handle
    > in there - right?


    Obviously. A member function can only be called on an object.
    Defining a pointer doesn't create an object of the pointed to
    type; all it creates is a pointer, initialized (or not) as you
    specified.

    The recommendation to pull out the debugger was not a good one.
    You don't use a debugger unless you know exactly what you're
    looking for, which means some understanding of the code already.
    Using a debugger here would only have been valid if you knew (or
    at least thought you knew) where the object was allocated and
    the pointer initialized; you would note the address returned
    from the new expression, and then verify that you still had the
    same address in the pointer here. Using a debugger without
    knowing exactly what you are looking for is a very bad reaction.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 28, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Lawatsch

    ios and other

    Philip Lawatsch, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    430
    Mike Wahler
    Jul 13, 2003
  2. Marc Schellens
    Replies:
    8
    Views:
    3,068
    John Harrison
    Jul 15, 2003
  3. Ron Natalie
    Replies:
    2
    Views:
    1,576
    WahJava
    Mar 2, 2005
  4. Steven T. Hatton

    std::ios::binary?

    Steven T. Hatton, Jul 28, 2005, in forum: C++
    Replies:
    104
    Views:
    3,040
    Steven T. Hatton
    Aug 7, 2005
  5. Replies:
    6
    Views:
    219
Loading...

Share This Page