how to format a binary file?

J

JasBascom

The problem is this.
If I had text in a text file e.g.
The cat is fat.
The cat is sick.
The cat may die.

then i use

outfile.write((char*) rec, sizeof(rec))

that same text appear as thus.

the cat is fat the cat is sick the cat may die.

rec is a union. outfile is in binary mode.
How do i format the outfile, or insert a next line statement before writing the
file?
 
T

Thomas Matthews

JasBascom said:
The problem is this.
If I had text in a text file e.g.
The cat is fat.
The cat is sick.
The cat may die.

then i use

outfile.write((char*) rec, sizeof(rec))

that same text appear as thus.

the cat is fat the cat is sick the cat may die.

rec is a union. outfile is in binary mode.
How do i format the outfile, or insert a next line statement before writing the
file?

Welcome to the world of character translations.
When a file is _not_ open in binary mode, character
translations may take place, such as converting '\n'
to '\r\n' and vice versa. Make sure that your
text has '\n' or "\r\n" in it. (See strcat, strcpy
and other str*() functions.)

Next time, please show us the union and the minimal
working code that reproduces the issue.

Also, do not block write unions or any other
structure. The proper method is to write each
member out. One method for doing this is that
the size of a structure/union/class may not be
the sum of the size of its members.

Here are some relevant sections of the FAQ:
http://www.parashift.com/c++-faq-lite/input-output.html
http://www.parashift.com/c++-faq-lite/serialization.html
http://www.parashift.com/c++-faq-lite/how-to-post.html
http://www.eskimo.com/~scs/c-faq/s2.html

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

JasBascom

I have used the strcpy function. And the '\n' statement along with null
terminators in the original copying of this text. what I need is the answer to
my query not more questions.

how do I format a binary file to accept '\n' etc?
 
D

David Harmon

that same text appear as thus.

the cat is fat the cat is sick the cat may die.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[15.7] Should I end my output lines with std::endl or '\n'?"

See also the topic
"[5.8] How do I post a question about code that doesn't work correctly?"
Nobody can guess how to get from where you are to where you want to be,
because you haven't really told us where you are. There is obviously
something happening between starting with text in a file and getting to
outfile.write().

It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
K

Karl Heinz Buchegger

JasBascom said:
I have used the strcpy function. And the '\n' statement along with null
terminators in the original copying of this text. what I need is the answer to
my query not more questions.

how do I format a binary file to accept '\n' etc?

And the answer is still the same: You don't. You simply write
a '\n' character to the file when you need it.

The trick with binary files is this: Whatever you write to that output
stream is written 1:1 to the file, no more, no less. So if you need
some special characters to appear in the file, you have to write them.

This is different to text mode, where the output stream takes care
of some special characters and the conventions used on the operating
system you use. Eg. a simple '\n' can be replaced by the output
stream with the sequence '\r' '\n' if the conventions on your
operating system require this. Same in the other direction: if
a text mode input stream reads the sequence '\r' '\n', it may replace
that with a simple '\n'.
 

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
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top