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

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well good for you. You go on doing what you're doing incorrectly. Personally I don't care that you do things wrong. After all, you're a professional. I however will attempt to show people the correct way to do things.


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

  2. #17
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    So show me what is wrong, oh guru of gurus! Show me how smart you are! Your FAQ tells me nothing about why my code is wrong.

    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

  3. #18
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Ehm... what's wrong with char c ?;

    do u want an unsigned char ? :O

  4. #19
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Quote Originally Posted by quzah
    I however will attempt to show people the correct way to do things
    actually u continued to say that the code is wrong.. can u write the best code ever for this kind of problem pls ?

  5. #20
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    garrr -please delete this post mr admin
    Last edited by Giant; 07-14-2005 at 07:22 AM.

  6. #21
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    use int, that's the right way.
    cause char cannot handle EOF

  7. #22
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    fgetc() returns an INT and EOF is an INT which means that when truncating it to a char you will never see EOF.

    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

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

    But... so it works only because ctrl Z (windows) can be contained in a char

    I mean.. if i run it on unix, where EOF is return ctrl Z, it should'n work.. right ?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your FAQ tells me nothing about why my code is wrong.
    I think the idea is this:
    1. feof() tests for EOF, but doesnt set it.
    2. fgetc() sets EOF.
    3. EOF is only set when fgetc() is called after all the data has been read.
    So if one were to use a loop that tests with feof(), the loop will run one more time than intended, i.e. extra iteration is when fgetc() sets and returns EOF.

    I dont think this is a problem with your code though, since you do check to return value of fgetc() within the loop.

    Ehm... what's wrong with char c ?;

    do u want an unsigned char ? :O
    Read Definition of EOF and how to use it effectively.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Quote Originally Posted by From EOF FAQ
    What I am not:
    A good programmer

    What I am:
    A guy who doesn't read FAQ
    hehe lol :P

    Actually i've already read that faq some months ago but i didn't remember ..
    hehe sorry :P

  11. #26
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by mitakeet
    So show me what is wrong.
    if the last line of the file have no '\n', then your program will miscount the sum of lines.
    fafa
    aaa
    afadf//no newline here
    your program will count it as it has only 2 lines. in fact, it has 3 lines.

    it's ok to use feof() here, but feof() isn't always right, it can cause problem easily.

  12. #27
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    given a program like this
    Code:
    #include <stdio.h>
    
    int main( void )
    {
    	int i = 0;
    	char buf[100];
    	FILE *fp = fopen( "a", "r" );
      
    	while ( !feof(fp) )	{
    	  fgets( buf, sizeof(buf), fp );
    	  printf( "Line %4d: %s", i, buf );
    	  i++;
    	}
    
    	return 0;
    }
    and the file a like this
    245a6
    a12
    ffff
    f221
    21aa//a newline here
    then the out put will be
    Line 0: 245a6
    Line 1: a12
    Line 2: ffff
    Line 3: f221
    Line 4: 21aa
    Line 5: 21aa
    see what's wrong?
    so the use of feof() is discourage

  13. #28
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    While you certainly have an argument for a logical flaw in counting the number of lines, I still fail to see how my code is incorrect if you were to look at it in isolation. If you simply want to count the number of linefeeds (or any other character), this code should work perfectly without any bugs.

    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. #29
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Yes your code is correct.
    What I want to say is that feof() can easily cause problems.

  15. #30
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I agree with your statement and the information in the FAQ, what I don't agree with is Quzah telling me I am wrong for using feof() and failing to back that statement up. If he is so sure I am wrong, where is the explanation? He couldn't even provide your explanation (which is perfectly correct given the OPs original question), just snide comments.

    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