Counting the number of lines in a text file - help

This is a discussion on Counting the number of lines in a text file - help within the C Programming forums, part of the General Programming Boards category; Hi all, I wanted to write a program which will count the number of lines in a text file. And ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Counting the number of lines in a text file - help

    Hi all,

    I wanted to write a program which will count the number of lines in a text file. And I tried to modify a example to do this. This is what I have written:

    Code:
    /* This program counts the lines in a text file */
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    
    {
    int ch;
    
    FILE *fp;
    
    long NumberOfLines = 0;
    
    
    if (argc != 2)
    
    {
    printf ("Usage: %s filename\n", argv[0]);
    
    exit(1);
    
    }
    
    if ((fp = fopen(argv[1], "r")) == NULL)
    
    {
    printf("Can`t open %s\n", argv[1]);
    
    exit(1);
    
    }
    
    while ((ch = getc(fp)) != EOF)
    
    {
    if (ch = "\n") NumberOfLines++;
    }
    
    fclose(fp);
    
    printf("File %s has %ld lines\n", argv[1], NumberOfLines);
    
    return 0;
    
    
    }
    It doesn`t work. It counts the number of characters.

    My question is how does the program understands when it comes to the end of the line? There`s EOF for end-of-file, is there sth similar for end-of-line?
    Thanks in advance for your answers.

  2. #2
    Registered User Baaaah!'s Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    23
    Have you tried 'fgetc', instead of 'getc'?

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Using fgetc to control a while loop is not good practice - read the FAQ.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    fgets() reads the file one line at a time, so just create a loop that counts the number of times fgets() returns something other than NULL pointer.
    Code:
    char line[1024];
    NumberOfLines = 0;
    while( fgets(line,sizeof(line),fp) != NULL)
       NumberOfLines++;

  5. #5
    Registered User Baaaah!'s Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    23
    Code:
    if (ch = "\n") NumberOfLines++;
    Shouldn't this be:

    Code:
    if(ch == '\n')
       NumberOfLines++;
    ?

    ahluka: I can't find what you're referring to in the FAQ's eerrrr.... couldn't give me a link could you ?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Using fgetc to control a while loop is not good practice - read the FAQ.
    That's feof(), not fgetc.
    There's nothing wrong with the loop as posted.

    > if (ch = "\n") NumberOfLines++;
    1. use == not =
    2. use '\n' for a char, "\n" is a 1-character string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Everybody,
    Thank you very much for your quick response. It was a great help for a novice like me.

  8. #8

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Further question on line counting challenge

    I worked through the line counting challenge problem.

    I almost have it but I am not clear on how I am to code for reading the file.
    I keep getting the

    cout<<"Input should be of the form 'count filename.txt'";

    So I am not sure how to modify the code to read my file

    file is named ten.txt (it has ten rows of characters.

    any advice?

    thanks
    CS

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Have you tried 'fgetc', instead of 'getc'?
    They do the same thing, except getc is a macro, so don't use it for code that has side effects, like this:
    Code:
    getc(*s++);
    cout is C++. What do you mean, you keep getting it?

    [edit]Here's the feof() link: http://faq.cprogramming.com/cgi-bin/...&id=1043284351[/edit]
    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.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You mean you want to change
    Code:
    if ((fp = fopen(argv[1], "r")) == NULL)
    into
    Code:
    if ((fp = fopen("ten.txt", "r")) == NULL)
    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.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oh, I know what you mean by the cout now. It's in that other link. You need to run that sample program like this:
    Code:
    C:\DWK>count ten.txt
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. counting words in a text file...
    By flightsimdude in forum C Programming
    Replies: 10
    Last Post: 09-19-2003, 07:02 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21