![]() |
| | #1 | |
| Registered User Join Date: Feb 2006
Posts: 127
| Line Counting Code: int linecount (FILE *file){
char ar[1024];
int count=0;
while(feof(file)==0){
fgets(ar,1024,file);
count++;
}
return(count);
}
~Sven
__________________ Windows XP Home Edition - Dev-C++ 4.9.9.2 Quote:
| |
| 00Sven is offline | |
| | #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 | |
| | #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 | |
| | #4 | |
| Been here, done that. Join Date: May 2003
Posts: 1,036
| Quote:
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 | |
| | #5 | |
| Registered User Join Date: Jan 2006 Location: Berkeley, Ca
Posts: 196
| Quote:
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 | |
| | #6 |
| and the hat of Jobseeking 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? |
| Salem is offline | |
| | #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 | |
| | #8 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #9 |
| Registered User Join Date: Jan 2006 Location: Berkeley, Ca
Posts: 196
| Code: while (fgets(buffer, MAXLINE, fp)) Code: while (fgets(buffer, MAXLINE, fp) != NULL) |
| cdalten is offline | |
| | #10 |
| Frequently Quite Prolix 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 ++;
__________________ 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 | |
| | #11 | |
| Sys.os_type="Unix";; Join Date: Aug 2005
Posts: 52
| Quote:
| |
| Sysop_fb is offline | |
| | #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 | |
| | #13 |
| Registered User 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++;
}
|
| Tonto is offline | |
| | #14 | |
| Registered User Join Date: Jul 2003
Posts: 111
| Quote:
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |