Thread: Beginner problem with counting lines in a file

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

    Beginner problem with counting lines in a file

    Hello, i'm just trying the challenge set on this site where i have to read a file and count the number of lines in this file. I've read several tutorials and FAQs on file handling but i still can't work out where i'm going wrong (it will be an extrememly simple error) I have the following code so far:

    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        int line = 0, c = 0;
        //char c;
        
        FILE *myfile_in;
        myfile_in = fopen(argv[1], "r");
        
        if (argc!=2)
        {
            printf("ERROR: Input should be in form fileio filename.txt");
        }
        
        if (myfile_in == NULL)
        {
           printf("ERROR: Invalid filename supplied ( %s )", argv[1]);
            return 1;
        }
        
        while(fscanf(myfile_in, "%d", &c) != EOF )
        {
            if (fscanf(myfile_in,"%d", &c) == '\n')
            {
                line++;
            }
        }
        fclose(myfile_in);           
        
        printf("Lines in file: %d", line);
        return 0;
    }
    The problem is that it says line is 0 every time. The text file contains a series of numbers on different lines, eg.

    Code:
    15
    20
    25
    30
    As usual no doubt i'm doing something totally wrong, so any input or tips/pointers are greatly appreciated. thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    %d in fscanf will read in numbers; comparing a number to a newline character will not produce the desired results. In fact, the newline in the file is simply being "bypassed" by fscanf() as it reads in the numbers in your file.

    Instead, use fgets(). There's a simple example here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    ... however even this version has some problems when you want to it to count lines. But don't worry about that now, just practice with it and see how you do.

    If you get stuck, I think there are a fair few posts about line counting programs on this forum. Try a search and see what you can find.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    I updated the code involved in counting the lines to:

    Code:
        while(fgets(buf, sizeof(buf), myfile_in) != NULL)
        {
                line++;
        }
    and the program now works (albeit a little slowly, damn my rubbish pc)

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh! Me too, me too!
    Code:
    int c, lines = 1;
    
    while( (c = fgetc( fp )) != EOF )
    {
        if( c == '\n' ) lines++;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problem with a loop reading lines of a file
    By PAragonxd in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2008, 12:57 AM
  3. A problem with reading multiple lines from a file
    By edd1986 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 01:26 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM