I need to write a function that counts the number of lines in an ordinary text file. How would i go about doing this?
I need to write a function that counts the number of lines in an ordinary text file. How would i go about doing this?
Did you manage to open the file yet? Just read through it, counting the number of \n's to give you a total. Post the code you're having trouble with and someone will help you.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
try this statementOriginally posted by duty11694
I need to write a function that counts the number of lines in an ordinary text file. How would i go about doing this?
well, try look at your refer book about the syntax code of the fscanf, i quite forget about the file processing file code. but the code that given above is correct but uncomplete. good luck.Code:.. .. .. while (the file ptr != EOF) { fscanf (....,"%[^\n]", ...); count++; } printf ("the counter is %d", count); .. .. ..
>>while (the file ptr != EOF)
This is not the way to control a loop that is reading from a file. It will end up causing problems (namely two loops at the end of the file). [edit] Actually, the FILE* will never see EOF, they are two different things.
>>fscanf (....,"%[^\n]", ...);
What's the matter with fgets() ? It's much safer.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Eeeeew. I myself prefer fgets, so much cleaner, or perhaps fgetc.Originally posted by beely
try this statement
Code:while (the file ptr != EOF) { fscanf (....,"%[^\n]", ...); count++; }
Just stick fgetc in a loop and every time it's a newline, increment the counter. Fgets requires an additional check, because potentially you could read a buffer full and not encounter a newline but the read itself would be successful...
Quzah.
Hope is the first step on the road to disappointment.
quzah, don't you think it would be a lot slower to have fgetc in the loop that fgets and just test for the '\n' char. you have to figure that using fgetc and singly testing for the \n you will have 5 - 10 times more iterations which = more time.
An how do you think the fgets function works?Originally posted by stallion
quzah, don't you think it would be a lot slower to have fgetc in the loop that fgets and just test for the '\n' char. you have to figure that using fgetc and singly testing for the \n you will have 5 - 10 times more iterations which = more time.
fgets: read at most n characters looking for a '\n'
yes, of course. the fgets loop has to go through x amount of times, but if we were trying to simulate fgets with our own loop then it would be more time consuming. i don't know, just a thought.
Why not write both versions, and then time them against a large input file, and see which is quickest.Originally posted by stallion
yes, of course. the fgets loop has to go through x amount of times, but if we were trying to simulate fgets with our own loop then it would be more time consuming. i don't know, just a thought.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Furthermore, do you really think scanning a text file to count newlines is a time critical application? It's not like it's dosing medicine in a hospital or something.Originally posted by Hammer
Why not write both versions, and then time them against a large input file, and see which is quickest.
Quzah.
Hope is the first step on the road to disappointment.
I know this isn't a function i was just testing out the code;
and it doesn't seem to work it compiles but it won't print out the number of lines in the text file. any ideas?
[\code]
#include <stdio.h>
int main (void)
{
FILE *inp;
int count;
inp = fopen("Text.txt", "r");
while (inp != EOF);
{
fscanf(inp, "%\n");
count++;
}
printf("The counter is %d", count);
fclose(inp);
return (0);
}
Of course it doesn't work. This isn't valid:
fscanf(inp, "%\n");
Quzah.
Hope is the first step on the road to disappointment.
what should it be then
quzah, a bit question to you about the speed execting the code between :Originally posted by quzah
Eeeeew. I myself prefer fgets, so much cleaner, or perhaps fgetc.
Just stick fgetc in a loop and every time it's a newline, increment the counter. Fgets requires an additional check, because potentially you could read a buffer full and not encounter a newline but the read itself would be successful...
Quzah.
>> scanf ("%[^\n]", <variable> ); OR
>> fgetc OR getc ?
so, i still wonder which one is more faster. if using scanf is much faster than the fgetc or getc and else..
>>so, i still wonder which one is more faster. if using scanf is much faster than the fgetc or getc and else..
Again... if you want to know which is faster, simply write both versions and time them. But hey, you really want to use scanf() over fgets() ??!! I think you shouldn't
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]