I'm reading lines off of text file and I'm wondering if this is a good way to go? I had to write the function numberoflines
to decrease the number_of_lines variable
by one because within the while loop, for every line it read it adds 2 to the number_of_lines variable.
#include <iostream>
#include <fstream>
using namespace std;
int number_of_lines = 0;
void numberoflines();
int main(){
string line;
ifstream myfile("textexample.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
number_of_lines++;
}
myfile.close();
}
numberoflines();
}
void numberoflines(){
number_of_lines--;
cout<<"number of lines in text file: " << number_of_lines << endl;
}
Is there any other easier better way?
See Question&Answers more detail:os