Click to See Complete Forum and Search --> : File io & mod


yawningdog
01-07-2003, 10:19 PM
Consider the following code.


// memberio - opening Files for read and write
#include <fstream.h>
int main()
{
char Name[80];
char info[255]; //for user input
int days = 1;
cout << "Member's name: ";
cin >> Name;

ofstream fout(Name); // open for writing
fout << Name << "\n";
fout << days << "\n";
cout << "Enter comments: ";
cin.ignore(1,'\n'); // eat the newline after the filename
cin.getline(info,255); // get the user's input
fout << info << "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen

ifstream fin(Name); // reopen for reading
cout << "Here's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;

cout << "\n***End of file contents.***\n";

fin.close(); // always pays to be tidy
return 0;
}


So far everything works like I need it to. output of the code looks like...

yawningdog
1
swell guy
***End of file contents***

What I want to do is retrieve the int days, increment it by 1 (++) and overwrite to the file. How do I do this?
I can probably parse it out with cin.peek, but there's got to be a better way.