problems reading from a file

Hi guys

I need to create a transaction log which will log all withdrawals and deposits made in a bank account program.

I have successfully got it to write to the file. And i have actually successfully got it to display all of the log from the text file once.

Heres my code:

ifstream infile("transactionLog.txt");

if(infile.is_open())
{
while(!infile.eof())
{
getline(infile,line);
cout << line <<
endl;
}
infile.close();
}

My program is menu driven. So when i choose option '1' for example, i would like the program to display all the text in the file.

Strangely, when i choose option '1' for the first time, it displays everything correctly. After the loop has finished, i go to display the text again but nothing happens. So to see the log i need to keep reloading the program basically.

So basically, i want to make it so i can display the log in the console as many times as i choose the option to do so and not only once and having to recompile and run the program.

Many thanks again.....
I don't see anything there that could be failing. How about posting code from after the decision. I mean after it's been determined that the selected option is 1.
yea sure.

while(1)
{
mgrMenu();
cout << endl << "ENTER A CHOICE: " << endl;
cin >> menuChoice;
cout << endl;
if(menuChoice == 1)
{
if(infile.is_open())
{
while(!infile.eof())
{
getline(infile,line);
cout << line << endl;
}
infile.close();
system("PAUSE");
system("CLS");
}
}
}
Hmm... Are you able to input anything? that cin >> menuChoice; might have choked on your CR.
Yes i am. When the loop reaches end of file. It returns back to the menu, and as i say, selecting option '1' again does not display the file contents :S. But i am able to select other menu options (not shown).
Last edited on
Oh!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(1){
	mgrMenu();
	cout << endl << "ENTER A CHOICE: " << endl;
	cin >> menuChoice;
	cout << endl;
	if(menuChoice == 1){
		//<-- Here -->
		if(infile.is_open()){
			while(!infile.eof()){
				getline(infile,line);
				cout << line << endl;
			}
			infile.close();
			system("PAUSE");
			system("CLS");
		}
	}

I see here that you're not opening the file again:
infile.open("transactionLog.txt");
Just tried that and now it gets stuck in an infinte loop lol. Any other suggestions?
Did you remove the constructor parameter when you declare infile?
I removed the constructor parameter when i declared infile. Tried it again and it would only display it once again :( lol
Last edited on
while(1) loops infinitely. Try something like: while(menuChoice != 'q') and use q to quit. Is that what you mean?
I'd like to see the complete source.
I have finally solved the problem!

all i was missing was infile.clear();

it now displays the log every time. :D

if(menuChoice == 1)
{
infile.open("transactionLog.txt");
while(infile.is_open())
{
while(!infile.eof())
{
getline(infile,line);
cout << line << endl;
}
infile.close();
infile.clear(); //missing from previous examples
system("PAUSE");
system("CLS");
}
}
Oh, clear()!
Topic archived. No new replies allowed.