473,396 Members | 2,023 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,396 software developers and data experts.

mix mode file reading/writing?

Hi, I have a file format that is going to contain some parts in ascii, and
some parts with raw binary data. Should I open this file with ios::bin or
no?

For example:
filename: a.bin
number of points = 123
@@@begin data@@@
gibberish follows.....

@@@end data@@@

I will use the >> and << operators for the ascii parts, and iostream's read
and write functions for the binary parts. Any advice or suggestions will be
appreciated. Thanks.

Smith
Jul 19 '05 #1
4 6384
"john smith" <as**@asdf123asdf.net> wrote in message
news:bg***********@news.eecs.umich.edu...
Hi, I have a file format that is going to contain some parts in ascii, and
some parts with raw binary data. Should I open this file with ios::bin or
no?

The only difference between text and binary reads are the lack of \r\n reads
for binary. binary reads consider the characters, which mean end of line for
text, potential values themselves. I would suggest opening the file in
binary mode (for most of your file-related coding).
--

Regards,
Greg P.

Golden Rule of Open Source Programming:
"Don't whine about something unless you plan to implement it yourself"
Jul 19 '05 #2
"Greg P." <no@spam.sam> wrote in message
news:fT****************@newsread3.news.pas.earthli nk.net...
"john smith" <as**@asdf123asdf.net> wrote in message
news:bg***********@news.eecs.umich.edu...
Hi, I have a file format that is going to contain some parts in ascii, and
some parts with raw binary data. Should I open this file with ios::bin or
no?

The only difference between text and binary reads are the lack of \r\n reads
for binary. binary reads consider the characters, which mean end of line for
text, potential values themselves. I would suggest opening the file in
binary mode (for most of your file-related coding).
--

Regards,
Greg P.

Golden Rule of Open Source Programming:
"Don't whine about something unless you plan to implement it yourself"


In addition, if you read a binary file in text mode, your program may
quit reading prematurely since it cannot detect the correct EOF position.

--
ES Kim
Jul 19 '05 #3
Hello, I still need some help with this.

When I open the file for writing in ASCII, it writes fine. Then I close it
and reopen it like so:
in.open(filename, ios_base::binary | ios_base:ate), and then I write my
data. When I open the file in notepad, the contents have become all
gibberish, like it's a binary file. But what I'm trying to do is something
like some Linux installation scripts. There is a text area which is human
readable, then it's followed by the binary area. The presumably the text
readable area is parsed by the shell and the binary area is executed. While
that's not what I'm trying to do, I am trying to write and read some raw
data, while being able to parse information about that data in raw text...

Any insights would be appreciated. Thanks in advance.

Smith

"ES Kim" <es***@svd.co.kr> wrote in message
news:bg**********@news1.kornet.net...
"Greg P." <no@spam.sam> wrote in message
news:fT****************@newsread3.news.pas.earthli nk.net...
"john smith" <as**@asdf123asdf.net> wrote in message
news:bg***********@news.eecs.umich.edu...
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no?

The only difference between text and binary reads are the lack of \r\n reads for binary. binary reads consider the characters, which mean end of line for text, potential values themselves. I would suggest opening the file in
binary mode (for most of your file-related coding).
--

Regards,
Greg P.

Golden Rule of Open Source Programming:
"Don't whine about something unless you plan to implement it yourself"


In addition, if you read a binary file in text mode, your program may
quit reading prematurely since it cannot detect the correct EOF position.

--
ES Kim

Jul 19 '05 #4
john smith wrote:
Hello, I still need some help with this.

When I open the file for writing in ASCII, it writes fine. Then I close it
and reopen it like so:
in.open(filename, ios_base::binary | ios_base:ate), and then I write my
data. When I open the file in notepad, the contents have become all
gibberish, like it's a binary file. But what I'm trying to do is something
like some Linux installation scripts. There is a text area which is human
readable, then it's followed by the binary area. The presumably the text
readable area is parsed by the shell and the binary area is executed. While
that's not what I'm trying to do, I am trying to write and read some raw
data, while being able to parse information about that data in raw text...

Any insights would be appreciated. Thanks in advance.

Smith


Please post your code and indicate where you are having problems.
Also state the expected behavior and the actual behavior.
This will allow people to assist you better.

--
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 19 '05 #5

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

Similar topics

17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
12
by: Aki Niimura | last post by:
Hello everyone, I started to use pickle to store the latest user settings for the tool I wrote. It writes out a pickled text file when it terminates and it restores the settings when it starts....
3
by: Alex Gerdemann | last post by:
Hello, I'm writing a program using the g++ compiler that runs under Cygwin in Windows, and have written the following: std::ifstream file; char buffer; std::string myString;...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
1
by: raghu | last post by:
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')-------->for writiing i.write('hai')--------->written some content in text file i =...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
10
by: Shraddha | last post by:
Why we should use binary mode of file? Where it is going to be actually needed???
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.