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

  1. #31
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It was covered in the FAQ. Everyone else here grasps the concept, just not you.


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

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It was covered in the FAQ. Everyone else here grasps the concept, just not you.
    quzah, I think you're wrong - it is not in the FAQ, because the reasoning in the FAQ doesnt apply as there is a check for the return value of fgetc(), although it was against '\n' instead of EOF.
    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

  3. #33
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Quote Originally Posted by quzah
    It was covered in the FAQ. Everyone else here grasps the concept, just not you.


    Quzah.

    When was the last time you read the FAQ you quote so often? Please show me where it specifically address the problem with my code.

    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. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem comes from idiots like you dragging on a discussion on how it's OK to use feof to control a loop, when such is not always the case. It's much easier to tell people to read the FAQ on the issue than it is to waste time with professionals like you in a debate of the times its "OK".

    It's not OK. There are better ways to do it. It's much easier on all parties involved if you don't waste time doing it the way that "sometimes works" than it is to simply use a way that works all the time.

    See, now I get to waste time with all of the other people that show up and say, "But I saw where it was OK!". Thanks.

    I'll just add you to the list of morons who like to use fflush( stdin ) because it works "sometimes", and all of the other pointless debates on things that "sometimes" work. Hell, "sometimes" when I play with pointers that I haven't initialized, it "works" too.


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

  5. #35
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    It is nice to know that you are a mature adult who is capable of carrying on a meaningful debate and not a childish twit who can only be rude and obstructive NOT.

    Instead of a useful comment you simply say my code is wrong and the FAQ tells me why, yet when I ask you for clarification all you do is stamp you feet and continue saying it is wrong and the FAQ is the answer (something that continues to your most recent post). How is it that my code 'sometimes works'? How about an example of when it wouldn’t work? What better way would you suggest for counting linefeeds in a stream? Instead of doing something useful with your awesome wisdom and experience, all you do is whine like a petulant child who won't share his bat and ball. I have seen this sort of behavior in many of your posts; is this purely because you have so many posts and are full of yourself? If you have lost your desire to help people, why hang around? Do you just like to insult people? Despite that I mostly help people at these forums, I also learn as well, even with more than 10 years of professional programming experience; why can’t you take the time to be helpful instead of an ass?

    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

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was being helpful. I showed two people in the same thread EOF/feof issues they weren't aware of. You're just being intentionally ignorant now. Feof shouldn't be used to control loops. But since you're the professional, do as you like.

    Had you really read the FAQ entry I mentioned, you would have seen a better way to do it. But since you're slow, I'll repeat it:
    Code:
    int c;
    while( (c = fgetc( fp )) != EOF )
        if( c == '\n' ) newline++;
    Now, this is better than your code for two reasons:
    1) We're not using feof to control a loop. Why start a bad habbit when there's a better way? Oh, but wait, that's not good enough for you. You're miffed because I don't like your code. Too bad.
    2) This version is twice as good as your code because this version only makes one function call per loop, while yours makes two. Twice as much work, just so you can use feof, which there is no point in using.

    But hell, you're a professional, so you know all about writing super code. Guess not.

    It's also apparent that you have some form of reading disability, because you don't seem to be able to read too well. I've explained why it's not a good idea to use it. The FAQ explains why it's not a good idea to use it. But like I said long ago, do whatever you like. No one here cares. Also, I really don't care if you write crappy code. But when you start saying it's a good way to do something, expect me to say otherwise.


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

  7. #37
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by mitakeet
    It is nice to know that you are a mature adult who is capable of carrying on a meaningful debate and not a childish twit who can only be rude and obstructive NOT.

    Instead of a useful comment you simply say my code is wrong and the FAQ tells me why, yet when I ask you for clarification all you do is stamp you feet and continue saying it is wrong and the FAQ is the answer (something that continues to your most recent post). How is it that my code 'sometimes works'? How about an example of when it wouldn’t work? What better way would you suggest for counting linefeeds in a stream? Instead of doing something useful with your awesome wisdom and experience, all you do is whine like a petulant child who won't share his bat and ball. I have seen this sort of behavior in many of your posts; is this purely because you have so many posts and are full of yourself? If you have lost your desire to help people, why hang around? Do you just like to insult people? Despite that I mostly help people at these forums, I also learn as well, even with more than 10 years of professional programming experience; why can’t you take the time to be helpful instead of an ass?
    Dont worry. As you said, you´re not the only one who thinks that.

  8. #38
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    on the button as usual mitakeet

  9. #39
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by mitakeet
    How is it that my code 'sometimes works'? How about an example of when it wouldn’t work? What better way would you suggest for counting linefeeds in a stream?
    Ignoring the blaze all around, I'll take a stab at it.

    Pretend I'm a noob and don't know any better. I've asked a programming forum about the best way to count all the new lines. Let's take your original code again:
    Code:
    while (!feof(f))
    {
    	if (fgetc(f) == '\n')
    		count++;
    }
    Sure, in this case, it works. The noob-me thinks it's awesome, but what happens when I later want to do a little more than just count newlines? Let's say I want to print every character. I might try something like this:
    Code:
    while (!feof(f))
    {
    	putchar(fgetc(f));
    }
    Looks reasonable to noob-me, but both you and I know that this is wrong.

    In my opinion, that is what is incorrect with using feof to control a loop. It can work, but the programmer has to remember to that EOF is set on a read and program accordingly. It's easier to demonstrate checking for EOF on the read, since it always works.

    [edit] I've got to agree with quzah...the information is all there in the FAQ.
    Last edited by pianorain; 07-15-2005 at 07:00 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

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