470,833 Members | 1,373 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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::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 ;)
Jun 27 '08 #1
9 3420
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
Jun 27 '08 #2
On Apr 25, 11:40 am, Ron Eggler <unkn...@example.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::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.

Jun 27 '08 #3
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::open
()
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? :o

Thanks,
Ron

--
weeks of software enineering safe hours of planing ;)
Jun 27 '08 #4
Christopher wrote:
On Apr 25, 11:40 am, Ron Eggler <unkn...@example.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::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::open
()
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? :oi I'm pretty much stuck here :(
Thanks,
Ron

--
weeks of software enineering safe hours of planing ;)
Jun 27 '08 #5
On Apr 25, 3:08 pm, Ron Eggler <unkn...@example.comwrote:
Christopher wrote:
On Apr 25, 11:40 am, Ron Eggler <unkn...@example.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::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::open
()
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.
Jun 27 '08 #6
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...@example.comwrote:
>Christopher wrote:
On Apr 25, 11:40 am, Ron Eggler <unkn...@example.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::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>
#>::open
()
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 ;)
Jun 27 '08 #7
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.
Jun 27 '08 #8
On Apr 25, 7:45 pm, Erik Wikstrm <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::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:ja*********@gmail.com
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
Jun 27 '08 #9
On Apr 26, 1:31 am, Ron Eggler <unkn...@example.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:ja*********@gmail.com
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
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Daniel Moree | last post: by
3 posts views Thread by Jon Slaughter | last post: by
103 posts views Thread by Steven T. Hatton | last post: by
9 posts views Thread by Use*n*x | last post: by
3 posts views Thread by katz911 | last post: by
6 posts views Thread by JackC | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.