writing binary file (ios::binary)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Eggler

    writing binary file (ios::binary)

    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::binar y |
    std::ios::trunc );" << std::endl;
    ofs->open(filename. c_str(), std::ios::binar y | 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::binar y | 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 ;)
  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: writing binary file (ios::binary)

    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::binar y |
    std::ios::trunc );" << std::endl;
    ofs->open(filename. c_str(), std::ios::binar y | 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::binar y | 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

    Comment

    • Christopher

      #3
      Re: writing binary file (ios::binary)

      On Apr 25, 11:40 am, Ron Eggler <unkn...@exampl e.comwrote:
      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::binar y |
      std::ios::trunc );" << std::endl;
      ofs->open(filename. c_str(), std::ios::binar y | 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::binar y | 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.



      Comment

      • Ron Eggler

        #4
        Re: writing binary file (ios::binary)

        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::binar y |
        >std::ios::trun c);" << std::endl;
        > ofs->open(filename. c_str(), std::ios::binar y |
        > 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::binar y | std::ios::trunc );
        >Segmentation fault (core dumped)
        >[/shell]
        >The permissions of folder /root/PRS_UPDATE look like:
        >root@NovaxPR G-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::binar y | std::ios::trunc );

        Program received signal SIGSEGV, Segmentation fault.
        [Switching to Thread -1211189568 (LWP 23001)]
        0xb7f36537 in std::__basic_fi le<char>::is_op en ()
        from /usr/lib/libstdc++.so.6
        (gdb) bt
        #0 0xb7f36537 in std::__basic_fi le<char>::is_op en ()
        from /usr/lib/libstdc++.so.6
        #1 0xb7edf2f6 in std::basic_ofst ream<char, std::char_trait s<char::open
        ()
        from /usr/lib/libstdc++.so.6
        #2 0x0804b189 in ?? ()
        #3 0x0804cae2 in ?? ()
        #4 0x0804a38d in ?? ()
        #5 0xb7d02050 in __libc_start_ma in () 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? :o

        Thanks,
        Ron

        --
        weeks of software enineering safe hours of planing ;)

        Comment

        • Ron Eggler

          #5
          Re: writing binary file (ios::binary)

          Christopher wrote:
          On Apr 25, 11:40 am, Ron Eggler <unkn...@exampl e.comwrote:
          >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::binar y |
          >std::ios::trun c);" << std::endl;
          > ofs->open(filename. c_str(), std::ios::binar y |
          > 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::binar y | std::ios::trunc );
          >Segmentation fault (core dumped)
          >[/shell]
          >The permissions of folder /root/PRS_UPDATE look like:
          >root@NovaxPR G-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::binar y | std::ios::trunc );

          Program received signal SIGSEGV, Segmentation fault.
          [Switching to Thread -1211189568 (LWP 23001)]
          0xb7f36537 in std::__basic_fi le<char>::is_op en ()
          from /usr/lib/libstdc++.so.6
          (gdb) bt
          #0 0xb7f36537 in std::__basic_fi le<char>::is_op en ()
          from /usr/lib/libstdc++.so.6
          #1 0xb7edf2f6 in std::basic_ofst ream<char, std::char_trait s<char::open
          ()
          from /usr/lib/libstdc++.so.6
          #2 0x0804b189 in ?? ()
          #3 0x0804cae2 in ?? ()
          #4 0x0804a38d in ?? ()
          #5 0xb7d02050 in __libc_start_ma in () from /lib/tls/i686/cmov/libc.so.6
          #6 0x080497a1 in ?? ()
          (gdb)
          [/GDB]
          Any other ideas? :oi I'm pretty much stuck here :(
          Thanks,
          Ron

          --
          weeks of software enineering safe hours of planing ;)

          Comment

          • Christopher

            #6
            Re: writing binary file (ios::binary)

            On Apr 25, 3:08 pm, Ron Eggler <unkn...@exampl e.comwrote:
            Christopher wrote:
            On Apr 25, 11:40 am, Ron Eggler <unkn...@exampl e.comwrote:
            {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::binar y | std::ios::trunc );
            >
            Program received signal SIGSEGV, Segmentation fault.
            [Switching to Thread -1211189568 (LWP 23001)]
            0xb7f36537 in std::__basic_fi le<char>::is_op en ()
            from /usr/lib/libstdc++.so.6
            (gdb) bt
            #0 0xb7f36537 in std::__basic_fi le<char>::is_op en ()
            from /usr/lib/libstdc++.so.6
            #1 0xb7edf2f6 in std::basic_ofst ream<char, std::char_trait s<char::open
            ()
            from /usr/lib/libstdc++.so.6
            #2 0x0804b189 in ?? ()
            #3 0x0804cae2 in ?? ()
            #4 0x0804a38d in ?? ()
            #5 0xb7d02050 in __libc_start_ma in () 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.


            Comment

            • Ron Eggler

              #7
              Re: writing binary file (ios::binary)

              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::open() 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 <unkn...@exampl e.comwrote:
              >Christopher wrote:
              On Apr 25, 11:40 am, Ron Eggler <unkn...@exampl e.comwrote:
              {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::binar y | std::ios::trunc );
              >>
              >Program received signal SIGSEGV, Segmentation fault.
              >[Switching to Thread -1211189568 (LWP 23001)]
              >0xb7f36537 in std::__basic_fi le<char>::is_op en ()
              >from /usr/lib/libstdc++.so.6
              >(gdb) bt
              >#0 0xb7f36537 in std::__basic_fi le<char>::is_op en ()
              > from /usr/lib/libstdc++.so.6
              >#1 0xb7edf2f6 in std::basic_ofst ream<char, std::char_trait s<char>
              >#>::open
              >()
              > from /usr/lib/libstdc++.so.6
              >#2 0x0804b189 in ?? ()
              >#3 0x0804cae2 in ?? ()
              >#4 0x0804a38d in ?? ()
              >#5 0xb7d02050 in __libc_start_ma in () 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 ;)

              Comment

              • Jim Langston

                #8
                Re: writing binary file (ios::binary)

                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::open() 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.


                Comment

                • James Kanze

                  #9
                  Re: writing binary file (ios::binary)

                  On Apr 25, 7:45 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
                  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::binar y |
                  std::ios::trunc );" << std::endl;
                  ofs->open(filename. c_str(), std::ios::binar y | 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::binar y | 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:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • James Kanze

                    #10
                    Re: writing binary file (ios::binary)

                    On Apr 26, 1:31 am, Ron Eggler <unkn...@exampl e.comwrote:
                    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::open() 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:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    Working...