Thread: Help with writing a function

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Help with writing a function

    I need to write a function that counts the number of lines in an ordinary text file. How would i go about doing this?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Re: Help with writing a function

    Originally 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?
    try this statement

    Code:
    ..
    ..
    ..
    while (the file ptr != EOF)
    {
      fscanf (....,"%[^\n]", ...);
      count++;
    }
    
    printf ("the counter is %d", count);
    ..
    ..
    ..
    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.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: Help with writing a function

    Originally posted by beely
    try this statement

    Code:
    while (the file ptr != EOF)
    {
      fscanf (....,"%[^\n]", ...);
      count++;
    }
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    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.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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.
    An how do you think the fgets function works?

    fgets: read at most n characters looking for a '\n'

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    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.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    Why not write both versions, and then time them against a large input file, and see which is quickest.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    Why not write both versions, and then time them against a large input file, and see which is quickest.
    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.

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

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Help with writing a function

    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);
    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it doesn't work. This isn't valid:

    fscanf(inp, "%\n");

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

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    need help

    what should it be then

  14. #14
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Re: Re: Re: Help with writing a function

    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.
    quzah, a bit question to you about the speed execting the code between :
    >> 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..

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Re: Re: Re: Help with writing a function

    >>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]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM