Writing at the EOF using ofstream.

Started by
4 comments, last by KurtCPP 20 years, 9 months ago
Hi everyone. First, please stop giggling cuz I still use ofstream, thanks. Then, Id like you to explain you, please, how I can make my buffer(or something like that) reach the end of a plain text file in order to write there instead of overwriting the previous contents of the file. Furthermore, if someone knows and can tell me about a structure similar to Pascal records to write in files then they''d be very kind to gimme their explanation. Thanks to all in advance. Prog, Sex & Rock''n''Roll : I don''t like the Prog but the Prog likes me.
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Advertisement
perhaps you should look at the arguments you can pass in to the ofstream constructor.

How appropriate. You fight like a cow.
There''s absolutely nothing wrong with using ofstream, and anybody that told you differently knows nothing.
daerid@gmail.com
Hey there,

to open a file and write to the end of it you want to open it like:

ofstream myFile("SomeFileName",ios::app);

Where app means append. So anything you write will be Appended to the end of the file. Now for writting records to files, you may want to go the route of writing out a structure in binary to a file.

//So take the followingstruct record{int ssn;string name;};//then in your program you would sayofstream fout("file.dat", ios::binary); //notice the ios::binary meaning you're writting to your file in binaryrecord example_record;example_record.ssn=12345;name="Mr.Bungle";fout.write((char *)(&example_record), sizeof(example_record));/*that would write the data to your file, then to read in you would go:*/record readin;fout.read((char *)(&readin), sizeof(readin));/*after you had opened such a file by going:*/ifstream fin("file.dat", ios::binary);/*Now if you already have data in the file and don't want to go binary then you may want to just write a function that outputs the information to a file. Like:*/outputrecord(record outputme, ofstream* fout){fout<<outputme.name<<endl;fout<<outputme.ssn<<endl;fout<<endl;}

Gamedev has a great tutorial on writting to files at this link:
http://www.gamedev.net/reference/articles/article1127.asp

It explains everything I've explained except much more clearly. Goodluck!
~Wave

[edited by - Wavewash on July 3, 2003 5:11:58 AM]
fstreams (of all kinds) are perfectly fine... what else are you gonna use? c style file handle thingies (not even sure what the hell theyre called)? i certainly hope not, its c++ =P, use an object, theyre your friends =)

Bungo!
Bungo!
Thank you all dudes!
Your help''s gonna be very useful for me!
(BTW actually the question was merely about the method to write at the EOF but be sure that I dont give a shit about those who could have told me not to use fstreams, I simply thought they had already got a bit too old-fashioned but thats exactly the reason why I chose''em to debug my fullscreen programs [and believe me dudes, that''s REALLY powerful!!])
Thanks again. Goodbye!

Prog, Sex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.

This topic is closed to new replies.

Advertisement