473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to format a binary file?

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?
Jul 22 '05 #1
4 1456
JasBascom wrote:
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-lit...alization.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

Jul 22 '05 #2
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?
Jul 22 '05 #3
On 11 Feb 2004 13:51:40 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
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/
Jul 22 '05 #4
JasBascom wrote:

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'.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
11
by: Dale | last post by:
How to recognize whether file has XML format or not? Here is the code segment: XmlDocument* pDomDocument = new XmlDocument(); try { pDomDocument->Load(strFileName ) ; } catch(Exception* e) {
6
by: Mark R.Bannister | last post by:
Hi, I'm currently designing a new language that compiles into a bytecode format. I have a choice: a) Design my own binary file format for the bytecode, symbols and data b) Use an existing...
9
by: gamehack | last post by:
Hi all, I've been wondering when I write a structure like: struct { int a; unsigned int b; float c; } mystruct;
6
by: coriolis_wong | last post by:
Hi, I need to transfer csv format file to DBase III format file. How do i do it in Python language? Any help is appreciated. Thanks.
6
by: Ted | last post by:
I used bcp to produce the apended format file. How can it be modified to recognize the quotes that surround the text fields and not insert the quotes along with the text? Invariably, the first...
1
by: ghadahelal | last post by:
hi all, i have proramm which reads a *.cap file i open this cap. file in binary format to read it, now i want to write also an output file in this format output.cap. i did that, i becam no errors...
2
by: Jen | last post by:
I have a Hashtable serializing to a binary file ok but I really would like the file to be in XML format or other readable format. I DO NOT want to change all my code, I want to keep the Hashtable...
6
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs =...
4
by: doublemaster007 | last post by:
What you mean by ARM-ELF file format? Is this a binary format? If so what is the binay format of AIX, HP, Suse?? Is this the right place to ask this? or can u suggest better place?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.