Thread: Count lines in file

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    6

    Count lines in file

    Hello, I need help with program that counts overall number of lines in file and the empty lines. I have problem with the part with counting. If anyone can help it will be great
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    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. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Code:
    	char buf[5000], *el, ch;
    	int counter = 0, nel = 0;
    
    
    	while( !feof(fp) ){
    		fgets(buf, 5000, fp);
    		el=buf;
    		while(*el==' ' || *el=='\t') el++;
    		if(*el=='\r') el++;
    		if(*el=='\n') counter++;
    	}
    This but it only counts the empty lines

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Dont use feof. It might be causing the problem.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    ok guys I made some short code for counting the overall lines
    Code:
    void countlines(char *fail4e){
    	int c, el=0, nel=0;
    
    	while(!feof(fp)){
    		c=fgetc(fp);
    		if(c=='\n') el++;
    	}
    	printf("%d", el);
    	system("pause");
    }
    Now I need to make it count the empty lines too. I dont like my old piece of code that uses arrays. Can you help me with that?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Xpload View Post
    Code:
    	char buf[5000], *el, ch;
    	int counter = 0, nel = 0;
    
    
    	while( !feof(fp) ){
    		fgets(buf, 5000, fp);
    		el=buf;
    		while(*el==' ' || *el=='\t') el++;
    		if(*el=='\r') el++;
    		if(*el=='\n') counter++;
    	}
    This but it only counts the empty lines
    You're over-thinking the problem. It's really quite simple. fgets() returns NULL when it reaches an error or END OF FILE.

    so a simple
    Code:
    while((fgets(blahblahblah)) != NULL) {
      counter++;
    }
    is all you need *IF* you're sure that the size of your buff[] will ALWAYS be larger than any single line of text, in the file.

    Which it is.

    Just get rid of all the other stuff, and you should be good to go. And don't count on feof any more - it's quite unreliable. Thankfully, it's also unnecessary.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    i still need to count the empty(may contain spaces and tabs) and non empty lines

  8. #8
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    You'll need 2 states; empty and non_empty. After a \n reset the state to empty, when reaching the first character thats not a \t or blank, set the state variable to non_empty, and check the state after a newline. If empty, increment it.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Well I know I need to read symbol by symbol and if I meet symb!='\t' || symb!=' ' I need to go to the next line and check it too. But I have no idea how this will look inco C code

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while character read is not EOF
        if char is newline
            line++
        lastchar = char
    if lastchar != newline
        line++
    Something like that should suffice.


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

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ok, I understand what you meant by "count the lines and empty lines".

    I took that to mean "I have to count all the lines, empty or not", which my code will do. What you meant was "I have to count AND report" the total empty and non empty number of lines.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here is an old thread on the topic that I'd found interesting (especially page 2):
    Line Counting
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    thank you all for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM