Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ofstream and binary files.

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Hey all...!

I found out a nice thing while going trough a friends C++ education disk.
It was 'ofstream writedata(filnamn,ios::binary|ios::app);' though I have little knowledge what it does I am quite sure it writes / reads data in binary form. I tried to write and I wrote in binary (I think). I searched the help files in BCB 3 but found nothing about ofstream
In my previous program I stored data in plain text, which I do not like due to the fact that one can easily manipulate that file.
The program main function is to store Internet connection time in a 'user'.txt file.
One file could look like this

#0,1125 // float number, the # is to find the float number [payment per minute]
¤SEK // string, the ¤ is to find the string [currency]
2000-09-01 02 30 // date (int)hours (int)minutes, hours and minutes connected that date
2000-09-02 00 20 // date (int)hours (int)minutes, hours and minutes connected that date


First I have a function that finds either '#' or '¤' and stores value in a label (or other) or if it finds a 'date' then it will get hours and minutes in that line and store them in a totalHours / totalMinutes integer, for now I store a row in the file as a string then gets two values at specific positions,

line=(AnsiString)row; // A row in the file using while(fget(row,81,file))
hour=line[12];
hour+=line[13]; // this would result in 02 in above files first 'date' row

I am unable to use SubString;

Can anyone help me recreate the function that gets stored information form a binary file?
I not so familiar with this 'ofstream writedata(filnamn,ios::binary|ios::app);'

One last question: When a binary file is opened with ofstream, can I then search for a specific value (numeric or string)? If so, How?

All suggestions are welcome!

Thanks in advance Martin!
[sig]<p>Martin G Broman<br><a href=mailto:mgb_svea@hotmail.com>mgb_svea@hotmail.com</a><br><a href= > </a><br>DWS - Alpha Whitin Dead Wolf Society[/sig]
 
[tab]ofstream is standard C++. You can probably find more info about that in a book about the C++ standard. (I'd recommend C & C++ Code Capsules by Chuck Allison and there are many others, too). What ofstream does is open an output file stream.ifstream does the opposite, it opens a file stream for input. Personally, I prefer streams to the C versions of fget & fput but many people prefer the C versions over streams. They accomplish the same thing.

[tab]The only difference between binary and &quot;text&quot; mode is how the end of the line is stored in the file. For example, in DOS text mode, file lines are appended with a &quot;\r\n.&quot; In binary mode, no translation is done. An ascii file is still readable with a text editor not matter whether it is &quot;binary&quot; or &quot;text.&quot;

[tab]If you want to &quot;encrypt&quot; a file, the easiest way it to XOR each character before putting it into a file. You will then need to XOR the characters again when you read the file.

[tab]To search for a specific value, you will need to parse the line that is read. You can do this either by looking for a specific number of characters or, it the line was stored with delimiters, for a specific number of delimiter. It will depend on how the line was stored.
[sig]<p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.[/sig]
 
I am not so familiar with XOR, but i asume it would take allot of coding to get it work correctly in my program!
But I will try it, thanks!

If i don't get i to work then i'll just go with ASCII file style! [sig]<p>Martin G Broman<br><a href=mailto:mgb_svea@hotmail.com>mgb_svea@hotmail.com</a><br><a href= > </a><br>DWS - Alpha Whitin Dead Wolf Society[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top