writing binary file (ios::binary)

R

Ron Eggler

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
 
E

Erik Wikström

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.
 
C

Christopher

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


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.
 
R

Ron Eggler

Erik said:
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
 
R

Ron Eggler

Christopher said:
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


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
 
C

Christopher

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.
 
R

Ron Eggler

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
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.
 
J

Jim Langston

Ron said:
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.
 
J

James Kanze

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).
 
J

James Kanze

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top