File I/O question... please help!
Please help me with the following code!! I want to have a file that contains a list of recipes, with the first line of the file being the number of recipes. To test out the file IO code, I wrote up this little test program, but it keeps overwriting the file!
How can I make it append the data to the end, but change the first line at the end?
Thanks so much!
sirSolarius
Code:
#include <iostream>
#include <fstream.h>
void main()
{
int *current, currenter;
currenter = 80;
current = ¤ter;
ofstream CoolFile("C:\\recipes.dat", ios::app);
if (CoolFile)
{
CoolFile << "I'm" << endl;
CoolFile << "One" << endl;
CoolFile << "Crazy" << endl;
CoolFile << "Guy" << endl;
CoolFile.close();
ofstream numberFile("C:\\recipes.dat");
numberFile.seekp(0);
numberFile << *current;
}
ifstream CoolFileIn("C:\\recipes.dat");
char stringer[100];
int x;
for (x=0; x<6;x++)
{
CoolFileIn >> stringer;
cout << "\n" << stringer;
}
}
Re: File I/O question... please help!
Quote:
Originally posted by sirSolarius
How can I make it append the data to the end, but change the first line at the end?
I don't really understand your question. You want to append data to the end, that is not touching the rest of the data, but you want the first line at the end? Huh?
Well I think your problem is:
Code:
ofstream CoolFile("C:\\recipes.dat", ios::app);
Try this:
Code:
ofstream CoolFile("C:\\recipes.dat", ios::ate);
Notice the ios::ate, it doesn't call ios::out so it won't overwrite the file. Or maybe that just won't work, I don't know. Maybe you could throw a ios::nocreate in there for good measure.
Good Tutorial on File I/O.
:)