Thread: file io problem
View Single Post
Old 01-08-2003, 04:55 PM   #1 (permalink)
yawningdog
Registered User
 
Join Date: Jan 2003
Posts: 2
yawningdog is on a distinguished road
file io problem

Consider the following code.

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.
yawningdog is offline   Reply With Quote