C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2006, 08:02 PM   #1
Registered User
 
00Sven's Avatar
 
Join Date: Feb 2006
Posts: 127
Line Counting

I have been looking and determined that there is no C function that counts the number of lines in a file. I would like to make a function to do this but do not really know how. So far I have this
Code:
int linecount (FILE *file){
	char ar[1024];
	int count=0;
	while(feof(file)==0){
		fgets(ar,1024,file);
		count++;
	}
    return(count);
}
This for some reason always seems to return 0 though. Can someone please help with this. Thanks.
~Sven
__________________
Windows XP Home Edition - Dev-C++ 4.9.9.2
Quote:
Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
board.theprogrammingsite.com
00Sven is offline   Reply With Quote
Old 03-31-2006, 09:11 PM   #2
Registered User
 
Join Date: Jan 2006
Location: Berkeley, Ca
Posts: 196
Maybe something like this

input file

line1
line2
line3
line4

Code:
/*Count number of lines*/

#include <stdio.h>
#include <stdlib.h>

#define BORED "/home/cdalten/cstuff/cstuff.txt"
#define MAXLINE 400

int linecount(FILE *fp) {
    char buff[MAXLINE];
    int count = 0;

    while(fgets(buff,MAXLINE,fp) != NULL) {
	count++;
    }
    return count;
}


int main(int arg, char **argv) {

    FILE *fp;

    int value = 0;

    if ((fp=fopen(BORED,"r")) != NULL) {
	  value = linecount(fp);
    }

    if(feof(fp)){
        printf("The value is: %d \n", value);
	return 0;
    }

    if(ferror(fp)){
	perror(argv[0]);
	return -1;
    }

    fclose(fp);
    return 0;
}

$gcc -Wall lc.c -o lc
$./lc
The value is: 4
$

Last edited by Salem; 04-01-2006 at 12:28 AM. Reason: Replace "colourful" example text with something neutral
cdalten is offline   Reply With Quote
Old 03-31-2006, 11:27 PM   #3
Registered User
 
Join Date: Mar 2006
Location: Bangalore, INDIA
Posts: 43
Hey, I think u can go for one more way of doing it by searching for "\n" and increase the count by one whenever u find "\n". This may work fine
enggabhinandan is offline   Reply With Quote
Old 04-01-2006, 12:24 AM   #4
Been here, done that.
 
Join Date: May 2003
Posts: 1,036
Quote:
Originally Posted by 00Sven
I have been looking and determined that there is no C function that counts the number of lines in a file. I would like to make a function to do this but do not really know how. So far I have this
Code:
int linecount (FILE *file){
	char ar[1024];
	int count=0;
	while(feof(file)==0){
		fgets(ar,1024,file);
		count++;
	}
    return(count);
}
This for some reason always seems to return 0 though. Can someone please help with this. Thanks.
~Sven
Your concept is fine, with a couple small problems.

Problem #1 is your use of feof(). See this FAQ for an explanation.

Next, what if a line is 1123 characters? fgets() will read the first 1023 and the next fgets() will read the last 100 characters. Test the last character in your buffer (using strlen()) for the \n and increase the count only if it's there.
__________________
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP is offline   Reply With Quote
Old 04-01-2006, 02:12 PM   #5
Registered User
 
Join Date: Jan 2006
Location: Berkeley, Ca
Posts: 196
Quote:
Originally Posted by WaltP
Your concept is fine, with a couple small problems.

Problem #1 is your use of feof(). See this FAQ for an explanation.

Next, what if a line is 1123 characters? fgets() will read the first 1023 and the next fgets() will read the last 100 characters. Test the last character in your buffer (using strlen()) for the \n and increase the count only if it's there.
I haven't test this, but wouldn't something like this also work

Code:
int linecount = 0; 
int lastchar = '\n'; 

while (fgets(buffer, MAXLINE, fp)) 
         if (strchr(buffer, lastchar)) 
             ++count; 
 
    if (ferror(fp)) { 
         fprintf(stderr, "Error reading the input file\n"); 
         exit(EXIT_FAILURE); 
     } 
 
   if (!strchr(buffer, lastchar)) 
         ++count;
cdalten is offline   Reply With Quote
Old 04-01-2006, 02:19 PM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,639
Why do you have two strchr() calls?
What does the last one do for you?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-01-2006, 02:30 PM   #7
Registered User
 
Join Date: Jan 2006
Location: Berkeley, Ca
Posts: 196
It was the first thing that came to my snoodle when I thought about treating any trailing characters without a new-line as a line.
cdalten is offline   Reply With Quote
Old 04-01-2006, 02:41 PM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,639
True, but you also have to contend with a file which may be empty as well.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-01-2006, 02:49 PM   #9
Registered User
 
Join Date: Jan 2006
Location: Berkeley, Ca
Posts: 196
Code:
while (fgets(buffer, MAXLINE, fp))
is the same as
Code:
while (fgets(buffer, MAXLINE, fp) != NULL)
If the file exists and there is nothing in the file, the code should fail. Right? Again, I didn't test this.
cdalten is offline   Reply With Quote
Old 04-01-2006, 03:08 PM   #10
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Yes, fgets() returns NULL if no characters were stored in the string before EOF was encountered.

This code:
Code:
while(fgets(line, sizeof(line), fp)) count ++;
is better than just counting newlines, because you can have a line in a file that isn't terminated by a newline. That is, if you have a file with the contents "foo", the fgets() method will claim the file has one line (which is probably what you want), whereas just counting the newlines would give you zero lines.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 04-01-2006, 05:01 PM   #11
Sys.os_type="Unix";;
 
Join Date: Aug 2005
Posts: 52
Quote:
Originally Posted by dwks
Yes, fgets() returns NULL if no characters were stored in the string before EOF was encountered.

This code:
Code:
while(fgets(line, sizeof(line), fp)) count ++;
is better than just counting newlines, because you can have a line in a file that isn't terminated by a newline. That is, if you have a file with the contents "foo", the fgets() method will claim the file has one line (which is probably what you want), whereas just counting the newlines would give you zero lines.
True but what happens if the line exceeds the array limits? It would continue to pull in data until it reached the new-line character each time doing a call to fgets and incrementing the counter each time as well?
Sysop_fb is offline   Reply With Quote
Old 04-01-2006, 07:13 PM   #12
Registered User
 
Join Date: Jan 2006
Location: Berkeley, Ca
Posts: 196
Hmm....I never thought about using sizeof(). Hahaha. Inexperience rears its ugly head huh?
cdalten is offline   Reply With Quote
Old 04-01-2006, 08:08 PM   #13
Registered User
 
Tonto's Avatar
 
Join Date: Jun 2005
Location: New York
Posts: 1,465
Well this is tough because we can't just check for like;

Code:
if(line[strlen(line) - 1] == '\n')
{
	count++;
}
Because of the aforementioned single line then EOF thing. Is there any sort of straight-forward solution? Is fgets a bad way to do this maybe?
Tonto is offline   Reply With Quote
Old 04-02-2006, 12:18 AM   #14
Registered User
 
Join Date: Jul 2003
Posts: 111
Quote:
...Because of the aforementioned single line then EOF thing. Is there any sort of straight-forward solution? Is fgets a bad way to do this maybe?
I don't see a compelling reason to use fgets here. After all, all versions posted so far simply discard the input. Using [f]scanf to discard the characters might yield a nicer solution.

The call scanf("%*[^\n]"); means "read all characters up to but *not* including the newline character and discard them". The '*' in this case means assignment suppression. A following call to getchar() removes the '\n' if it was there, otherwise it simply does nothing and evaluates to EOF.

Any solution should handle:
1) Empty files
2) Files with blank lines
3) Files with ill-formed last lines (no newline character before EOF)
4) Well formed last lines (newline character appears before EOF)

Perhaps even more, but I can't think of any off the top of my head.

HTH,
Will
whoie is offline   Reply With Quote
Old 04-02-2006, 12:57 AM   #15
Been here, done that.
 
Join Date: May 2003
Posts: 1,036
fgets() is the way to do this. After you read a line:
1) if the end of the text read ends with \n, count it. You read a line.
2) if the end of the text read does not end with \n, don't count it. You read the start but not the end of the line. The input buffer will be full in this case.
3) if you get EOF, you have reached the end of the file, exit the read loop. The input buffer still has the previous line read. Now test the end character again. If it does not contain a \n, count it. It's the last line.
__________________
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Imposing Line Numbers automatically in the C code cavestine C Programming 14 10-15-2007 12:41 AM
Adding Line numbers in Word Mister C A Brief History of Cprogramming.com 24 06-24-2004 08:45 PM
Trouble replacing line of file Rpog C Programming 4 04-19-2004 10:22 AM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM
follow up with char I/O and line, word, and char counting Led Zeppelin C Programming 1 03-23-2002 09:26 PM


All times are GMT -6. The time now is 04:07 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22