CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Posts
    14

    How to calculate total line number of a text file

    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?

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: How to calculate total line number of a text file

    Code:
    int c;
    while((c=fgetc(f))!=EOF)
      if(c=='\n')
        ++num_lines;
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: How to calculate total line number of a text file

    Use ifstream and its getline function.
    For each successful getline function call increment a variable.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    Aug 2004
    Location
    Chennai, India.
    Posts
    380

    Talking Re: How to calculate total line number of a text file

    Write code like below..

    Code:
    	CStdioFile sampleFile;
    	sampleFile.Open( "\\...myfile.txt", CFile::modeRead );
    
    	long length = 0;
    	CString temp("");
    	while( sampleFile.ReadString( temp ) )
    	{
    		length++;
    	}
    
    // length contains your line count..
    Thanks,
    dwurity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured