Thread: C help required

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    Unhappy C help required

    Hello,
    I am making a c program, and i was wondering if anyone knew of a way of counting the number of lines in a text file. I have files with numbers in it, but i dont know how many lines there will be, e.g

    23, 23.4, 2.445
    756, 5467, 56
    ...
    ...

    However, I know that each line is termnminated using \n so by counting the number of times that '\n' is written i could find out the total number of rows.

    Any ideas?
    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rmc View Post
    However, I know that each line is termnminated using \n so by counting the number of times that '\n' is written i could find out the total number of rows.
    That is one way; another would be if you are reading the file in line by line (eg with fgets) you could count lines that way.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    Thanks for the reply.
    Unfortunately im very new to programming, how could you use fgets exactly.
    All im doing is storing the data in an array, but i dont know how big to make the array unless i know the number of rows.
    ???

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You use fgets like it says how to in the manual.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    i am very sorry for that............my apologies...........i am new to the forum and i dont know how to start a new thread...........please help me in doing so..............i will be very thankful

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rmc
    Unfortunately im very new to programming, how could you use fgets exactly.
    You can search the Web, e.g., you might find cppreference.com's entry on fgets.

    Quote Originally Posted by rmc
    All im doing is storing the data in an array, but i dont know how big to make the array unless i know the number of rows.
    Unfortunately, storing an arbitrary number of lines (using just the C standard library) is probably too advanced for you at the moment. Likewise, storing an arbitrarily long line (using just the C standard library) is also probably to advanced for you at the moment.

    If you just use fgets(), you have to assume that the lines have some maximum length. One way to avoid this need for an unknown amount of space is to read character by character, and increment your count whenever you encounter a newline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by csvraju View Post
    i am very sorry for that............my apologies...........i am new to the forum and i dont know how to start a new thread...........please help me in doing so..............i will be very thankful
    tabstop is not trying to be dictatorial (we hope); you should consult some documentation first and then if you have problems using a command, etc. ask for help.

    So, in your current circumstance: how are you reading the file who's lines you'd like to count? (ps. before you post code read this)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    Well i originally tried it this way, but it doesn't work.

    Code:
    while (fscanf(myf,"%*[^\n]\n",&line)!=EOF)
        {  
           sum += line; 
        }
    Any ideas why?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on what you mean by "sum", "line", and "doesn't work".

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    Well, i thought that the sum would be incremented by 1 after each \n, however the sum is not equal to the total number of lines.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's easier to pinpoint a problem in this procedure if you use fgets() to get a line from the file and sscanf() on the line to extract your data. So for example:
    Code:
    char line[64];  /* should be long enough for this, right? */
    float data;
    FILE *fh;  /* obviously this should be fopend, etc. */
    while ((fgets(line,8,8,fh))) {
        sscanf(line, "%f", &data);
    }
    The outer layer of brackets is a truth test the same as this:
    Code:
    while (fgets(line,8,8,fh)!=NULL) {
    Since fgets returns NULL at end-of-file.

    Once you have everything worked out you can always change it back to the straight fscanf().

    For counting the lines add this to the loop:
    Code:
    sum++;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rmc View Post
    Well, i thought that the sum would be incremented by 1 after each \n, however the sum is not equal to the total number of lines.
    Why should line be 1? Why wouldn't it be whatever number you read in from the file?
    Also, it would get added to after every number, not every line.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    So, in your current circumstance: how are you reading the file who's lines you'd like to count? (ps. before you post code read this)[/QUOTE]


    Code:
    FILE *fp;
    fp = fopen("U:\\data.txt", "rt");

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rmc View Post
    So, in your current circumstance: how are you reading the file who's lines you'd like to count? (ps. before you post code read this)


    Code:
    FILE *fp;
    fp = fopen("U:\\data.txt", "rt");
    Okay, that's how you open the file. How are you reading the file?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Okay, that's how you open the file. How are you reading the file?
    The OP has admitted to fscanf().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. is this required in a function body?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 03:20 PM