Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > writing binary file (ios::binary)

Reply
Thread Tools

writing binary file (ios::binary)

 
 
Ron Eggler
Guest
Posts: n/a
 
      04-25-2008
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
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      04-25-2008
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
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      04-25-2008
On Apr 25, 11:40 am, Ron Eggler <(E-Mail Removed)> 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.



 
Reply With Quote
 
Ron Eggler
Guest
Posts: n/a
 
      04-25-2008
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> >: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?

Thanks,
Ron

--
weeks of software enineering safe hours of planing
 
Reply With Quote
 
Ron Eggler
Guest
Posts: n/a
 
      04-25-2008
Christopher wrote:

> On Apr 25, 11:40 am, Ron Eggler <(E-Mail Removed)> 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> >: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? i I'm pretty much stuck here
Thanks,
Ron

--
weeks of software enineering safe hours of planing
 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      04-25-2008
On Apr 25, 3:08 pm, Ron Eggler <(E-Mail Removed)> wrote:
> Christopher wrote:
> > On Apr 25, 11:40 am, Ron Eggler <(E-Mail Removed)> 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> >: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.


 
Reply With Quote
 
Ron Eggler
Guest
Posts: n/a
 
      04-25-2008
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: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 <(E-Mail Removed)> wrote:
>> Christopher wrote:
>> > On Apr 25, 11:40 am, Ron Eggler <(E-Mail Removed)> 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>
>> #>: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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-26-2008
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: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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-28-2008
On Apr 25, 7:45 pm, Erik Wikstrm <(E-Mail Removed)> 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:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-28-2008
On Apr 26, 1:31 am, Ron Eggler <(E-Mail Removed)> 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: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:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: working with binary files/extract png from a binary file Jim Ruby 6 12-24-2013 08:09 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Writing Numbers to a Binary File rob C++ 5 02-16-2004 02:15 AM
writing hex to a binary file Caryn Graves C++ 6 01-23-2004 09:31 PM
Writing a class (with data) to a binary file Sebastian Kemi C++ 6 09-21-2003 01:27 AM



Advertisments