hi , is there a way to determine how many lines are in a file?
hi , is there a way to determine how many lines are in a file?
open the file in textmode
initialialise a counter.
get ch using fgetc
increment counter on each '\n
int lines=0;
char ch;
fp=fopen("file.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if (ch=='\n') { lines++; }
}
fclose(fp);
printf("lines are %d",lines);
>open the file in textmode
>initialialise a counter.
>get ch using fgetc
>increment counter on each '\n
What if the file doesn't end with a newline? This would work a little better for that case:
But then you have the problem of lines that are longer than the buffer. So one might fall back to the reading of a single character with a few additions to handle end-of-file:Code:#include <stdio.h> int main(void) { FILE *fp; int lines = 0; char buffer[BUFSIZ]; fp = fopen("file.txt", "r"); while (fgets(buffer, sizeof buffer, fp)) { ++lines; } fclose(fp); printf("lines are %d\n", lines); return 0; }
Code:#include <stdio.h> int main(void) { FILE *fp; int lines = 0; char ch; fp = fopen("file.txt", "r"); for (;;) { ch = getc(fp); if (ch == '\n') { ++lines; } if (ch == EOF) { /* Assume the end */ ++lines; break; } } fclose(fp); printf("lines are %d\n", lines); return 0; }
Time for me to play forum police again.Originally Posted by sunil21
1) Don't post code at all, ever, if you aren't going to use [code] tags!
2) You would have known this, had you actually read the forum Announcements like you were supposed to.
Go read all of the forum Announcements, and abide by them, or don't bother posting.
Now on to some other problems...
Welcome to one of the most common bugs on the forum. EOF can never be stored in a char. Use an int.Originally Posted by Robc
(That is, after all, why getc returns an int and not a char. )
[edit]Didn't like the way the lines were wrapping...[/edit]
Quzah.
Last edited by quzah; 07-14-2004 at 02:44 PM.
Hope is the first step on the road to disappointment.
>Welcome to one of the most common bugs on the forum. EOF can never be stored in a char. Use an int.
ACK! Okay, I'm never showing my face here again. I can't believe I did that. This account makes me stupid, I think I'll go back to the old one even though it's broken.
This is code from GNU wc for linecounting:
safe_read is a call to read() that accounts for the fact that read returns (-1) when there are conditions that are not usually considered errors - e.g., EINTR.Code:while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0) { register char *p = buf; if (bytes_read == SAFE_READ_ERROR) { error (0, errno, "%s", file); exit_status = 1; break; } while ((p = memchr (p, '\n', (buf + bytes_read) - p))) { ++p; ++lines; } bytes += bytes_read; } }
>>fp = fopen("file.txt", "r");
... and don't forget to error check the value of fp before using it.
To the OP: Out of interest, why do you need to know the number of lines anyway? Is this just an exercise, or do you have real requirement to know this value?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>... and don't forget to error check the value of fp before using it.