Thread: How to get the number of lines in a file

  1. #1
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18

    Unhappy How to get the number of lines in a file

    Hi all...

    How can I get the numer of lines in a file ?

    A solution is:

    char string[20
    int counter = 0;

    f = fopen ("blabla.txt", "r");

    while (fscanf(f, "%s", string)>0) counter++;


    But.. is there a better solution ? one which doesn't waste memory allocating a variable ?

    PS:

    while (fscanf(f, "%*s")>0) counter++; doesn't work


    PPS: I'm Using Borland C++ Builder

    Thx in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well first off, your answer wouldn't work right even if it did compile. All it does is count "words". If it didn't run off the end of the buffer and crash your program, it still wouldn't give the right answer.

    Read a character at a time. Test the character to see if it's the newline character. If it is, you've reached the end of a line. Repeat until you find the end of the file. While you're at it, go read the EOF FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    How about (untested):

    Code:
    while (!feof(fin)){
       if (fgetc(fin) == '\n') lineCount++;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Then how about that line be
    Code:
    lineCount += (fgetc(fin) == '\n')

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mitakeet
    How about (untested):

    Code:
    while (!feof(fin)){
       if (fgetc(fin) == '\n') lineCount++;
    }
    How about reading the feof FAQ to see why your answer is wrong?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Quote Originally Posted by quzah
    Well first off, your answer wouldn't work right even if it did compile.
    well.. yes.. i already know how my file is formatted (one string per line)... however it's better to code a general case, which will work for every case

    _________________________________________________
    Code:
    while (!feof(f)){
      counter += (fgetc(f) == '\n');
    }
    works... and obviously the mitakeet one too




    Thx a lot guys

    Cya
    Last edited by delphi; 07-14-2005 at 05:48 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider reading the FAQ on how to get a line from the user. You're not seeing the point here: %s stops at white space. A "string" in C is a series of characters terminated by a newline. By definition then, it could include spaces. You don't say if your file has spaces other than the newlines at the end of the file. If you do, then %s isn't going to read the whole line. Likewise, if any of your "strings" is longer than your array size (including room for the null), you can crash your program.

    At any rate, you may want to visit the FAQ anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    well.. ok.. feof shouldn't be used.. then i can use this

    Code:
    char c;
    
    while ((c = fgetc(f))!=EOF){
    
       count += (c == '\n');
    
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nope. Some one didn't read the EOF FAQ like I suggested.

    On an aside, it'd likely be more efficient to test seperately from incrementing so you don't bother adding 0 over and over. Faster to check and if true increment, than to test and add for every character.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Since i'm noob.. could u write the best code in your opinion :O

  11. #11
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    char c;

    Code:
    while ((c = fgetc(f))!=EOF){
    
       if (c=='\n')
       count++;
    
    }
    is it ok now ?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char c;
    Nope. Someone still didn't read the EOF FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Quote Originally Posted by quzah
    How about reading the feof FAQ to see why your answer is wrong?


    Quzah.

    Care to explain to a dense 10 year professional programmer how my code is wrong? Your FAQ doesn't tell me how my code is flawed.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I guess a "10 year professional" can't read.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Quote Originally Posted by quzah
    I guess a "10 year professional" can't read.


    Quzah.

    I have in fact read your FAQ and I see nothing that changes my mind. Care to point out where I am so wrong, you being so smart and all?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 05:12 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM