while (inFile) loop won't terminate

Hi there,

I have written a program with a "while (inFile)" loop that won't terminate. Relevant code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main()
{
	ifstream inFile;
	ofstream outFile;
	int words, lines, paragraphs;
	char ch;

	initialize(words, lines, paragraphs);

	inFile.open("C:\\Users\\Joel\\Documents\\Visual Studio 2010\\Projects\\C++ Exercises\\Ch07\\7-7\\Input.txt");
	outFile.open("C:\\Users\\Joel\\Documents\\Visual Studio 2010\\Projects\\C++ Exercises\\Ch07\\7-7\\Output.txt");

	inFile.get(ch);

	while (inFile)
	{
		outFile << ch;

		if (ch == ' ')
			processBlank(words, ch, inFile, outFile);

		else if (ch != '\n')
			copyText(ch, inFile, outFile);

		else if (ch == '\n')
			updateCount(words, lines, paragraphs);

		inFile.get(ch);
	}

	inFile.close();

	printTotal(words, lines, paragraphs, outFile);
	outFile.close();

	return 0;
}


I don't understand why the while loop doesn't stop once the end of the input file has been reached. What happens currently is the very last character read from the input file keeps being printed over and over (and the printTotal function is never reached). Am I missing syntax to designate a break for the end of the file? Does a .txt file not have a marker at the end of the file to trip the loop to cease? Any advice would rock.

Thanks,
Warik
Last edited on
Topic archived. No new replies allowed.