Do I need to create a counter for input records?

Is there a system counter of the number of valid input records read? I want to be able to output the number of records read without creating a counter, if possible.

For example, the input file used with the program below has 160 records, but the "InputRecord" field ends up with more than 160 every time, depending on the "NumberUp" field. I know how to fix it, but I have a feeling that my "InputRecord" counter is unnecessary.

Thanks for the help.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	char InFileName[80] = "";
	char OutFileName[80] = "";
	int NumberUp = 1;
	cout << "\nWhat is the Input Filename? ";
	cin >> InFileName;
	cout << "\nWhat is the Output Filename? ";
	cin >> OutFileName;
	cout << "\nInput the Repeat number: ";
	cin >> NumberUp;

	string lineBuffer;
	ifstream inMyStream (InFileName);
	ofstream outMyStream (OutFileName,ios::app);

	if (inMyStream.is_open()){
		if (outMyStream.is_open()){
		int InputRecord = 0;
		int OutputRecord = 0;
		while (!inMyStream.eof()){
			int Counter = 0;
			while (Counter < NumberUp){
				getline (inMyStream, lineBuffer);
				InputRecord++;
				Counter++;
				outMyStream << lineBuffer;
				if (Counter != NumberUp && !inMyStream.eof()) outMyStream << ",";
			}
			if (!inMyStream.eof()){
				outMyStream << endl;
				OutputRecord++;
			}
		}
		if (NumberUp == 1){
			cout << "/nProcessing the Input File 1-up simply duplicates it." << endl;
			cout << "Rerun the program if this was an error." << endl;
		}
		else{
			cout << "/nProcessing Complete: " << NumberUp << "-Up Program." << endl;
		}
		cout << "Total Input Records: " << InputRecord << endl;
		cout << "Total Output Records: " << OutputRecord << endl;

		inMyStream.close();
		outMyStream.close();
		}
		else cout << "Error: Open Output File." << endl;
	}
	else cout << "Error: Open Input File." << endl;

	cin.ignore(2);
	return 0;
}
I think that you need it. The stream isn't going to tell you how many records you've read from it.
I changed the code to fix it:
if (!inMyStream.eof()) InputRecord++;

My question was whether or not there was an internal counter of records read. Is there an ifstream function that keeps a count of valid records?

"inMyStream.eof" determines whether the end of file is reached
"inMyStream.is_open" shows whether the file is open

so is there something like "inMyStream.valid_records_read" function? :D
Topic archived. No new replies allowed.