Thread: # of lines in a file...

  1. #1
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859

    # of lines in a file...

    how do I find it out?
    Yoshi

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    std::ifstream fin("file.txt", std::ios::in);
    
    int lines = 0;
    char c;
    
    do 
    {
       fin.get(c);
       if (c == '\n')
          ++lines;
    } while (!fin.eof());

  3. #3
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    thanks!
    Yoshi

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Code:
    std::ifstream fin("file.txt", std::ios::in);
    
    int lines = 0;
    char c;
    
    do 
    {
       fin.get(c);
       if (c == '\n')
          ++lines;
    } while (!fin.eof());
    That's not only inefficient, it's wrong. Using fin.eof() as the loop condition means you'll read one past eof, also what happens if the last line doesn't end with a newline? Then the number will be off. It's better to read whole lines instead of testing characters.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream fin("test.txt");
        
        int lines = 0;
        string line;
        
        while (getline(fin, line))
            lines++;
        
        cout << "There are " << lines << " lines" <<endl;
        
        return 0;
    }

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Inefficient? I found reading a single character to be much quicker on my system.

    Your way is also flawed. If the file doesn't end in a '\n', you increment lines anyway.

    Here's a modified method which seems to be more efficient, and correct:
    Code:
    std::ifstream fin("file.txt", std::ios::in);
    char c;  
    int lines = 0;
    while (fin.get(c))
    {
       if (c == '\n')
          lines++;
    }
    fin.close();

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    I found reading a single character to be much quicker on my system.
    That's between you and your system, buffered input is almost always faster than character by character input.
    If the file doesn't end in a '\n', you increment lines anyway.
    That was my point entirely, your way printed 2 with a 3 line file that didn't end in a newline, my way printed 3, as it should.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Crimpy
    That's between you and your system, buffered input is almost always faster than character by character input.

    That was my point entirely, your way printed 2 with a 3 line file that didn't end in a newline, my way printed 3, as it should.
    Why would my system be an exception?
    Then I guess our definitions of lines differ.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Why would my system be an exception?
    How the hell should I know? All I know is that of several different operating systems I've used, all of them run faster with buffered input than with character by character. Or to spell it out since you don't seem to understand, one call to getline is faster than lots of calls to get.
    Then I guess our definitions of lines differ.
    No, you're just not getting it.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    How the hell should I know?
    I don't know, but it seems vVv does.

    No, you're just not getting it.
    To go along with your tone, how the hell would you know? I get it completly, go back and read my post.

    Now, take a few deep breaths.

  10. #10
    dragunsflame
    Guest

    Question

    doesn't the getline() function go through every character (like the aforementioned code) looking for a '\n'? If so, then it probably would be quicker to do it yourself. of course, the getline() function might be optimized w/ different stuff (assembly perhaps?).

    or, i could be wrong. if so, go easy on me.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Then I guess our definitions of lines differ.
    If a file has one sentence in, but it that doesn't end in a \n, how many lines are in the file?

    Personally, I'd want the answer to be 1, as 0 just seems wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM