How to calculate total line number of a text file?
I have a text file. I want to get the total line number of it. Could somebody know which function should I use? Or I should write the counting code for each line?
Printable View
How to calculate total line number of a text file?
I have a text file. I want to get the total line number of it. Could somebody know which function should I use? Or I should write the counting code for each line?
Code:int c;
while((c=fgetc(f))!=EOF)
if(c=='\n')
++num_lines;
Use ifstream and its getline function.
For each successful getline function call increment a variable.
Write code like below..
Thanks,Code:CStdioFile sampleFile;
sampleFile.Open( "\\...myfile.txt", CFile::modeRead );
long length = 0;
CString temp("");
while( sampleFile.ReadString( temp ) )
{
length++;
}
// length contains your line count..
dwurity