counting number of lines in my output file

This is a discussion on counting number of lines in my output file within the C Programming forums, part of the General Programming Boards category; hey guys, im trying to use malloc and realloc in my programme, as the sizes of my arrays change with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    Exclamation counting number of lines in my output file

    hey guys,

    im trying to use malloc and realloc in my programme, as the sizes of my arrays change with some parameters in my program.But to use them i need to calculate the number of lines that I will have in my output file in the end, so that i can allocate the right amount of memory.But how could i go about writing the code for calculating the number of lines.

    in my output file, the first line lists some constants..so thats line number 1..then underneath i have two colums of time against x(t) which are both arrays..

    Merry Christmass!!
    xx

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Open file for sequential reading (see fopen for more information) and read line-by-line and count until you reach the end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If your lines are all exactly the same length, you can get the file size, then divide by the line length, but most files use variable length lines, so you'll probably have to do what Elysia suggested.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    heyy, thanx for the replies but i wanted to write it into my program a short piece of code that would calculate the number of lines in my output file, which varies all the time, say 'n' to then allocate the right amount of memory using malloc

    for e.g

    x = (float*)malloc(n*sizeof(float)) where x is my pointer to the array

    opening the file and reading the number of lines myself would not be of any use,as i need to know it beforehand to allocate the required memory. I read somewhere something like

    while(fprintf(output, "%f", &x) ==1) n++;

    but dont really understand that properly.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Quote Originally Posted by zesty View Post
    opening the file and reading the number of lines myself would not be of any use,as i need to know it beforehand to allocate the required memory. I read somewhere something like
    No, you don't. Just use a sizeable array of say 10k or so and read line-by-line to find out how many lines there are.
    If you just need to find the size, you can just open it, seek to the end (fseek) and retrieve the file pointer position (ftell).
    There's nothing preventing you from reading the file and deciding how much memory to allocate before reading it into memory at a later time.

    Alternatively, you can use a dynamic array - extend your array size as it needs to grow (realloc).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not malloc() a reasonable size to start, then realloc() if you need to make it bigger...?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    int c, lines = 0;
    FILE *f;
    
    ...
    
    /* Assume f points to a valid FILE opened for reading... */
    
    while((c = fgetc(f)) != EOF)
    {
        if(c == '\n')
        {
            lines++;
        }
    }
    Easier than you may think.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    But also more inefficient than reading line-by-line with fgets.

    Code:
    int lines = 0;
    char buffer[10000];
    FILE *f;
    
    ...
    
    /* Assume f points to a valid FILE opened for reading... */
    
    while( fgets(buffer, sizeof(Buffer), f) )
    {
    	lines++;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    But more accurate then using fgets without checking for '\n' in the returned string.
    No matter how big you make the buffer, there might be a line that is longer then that.
    Kurt

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd say that's coming close to abusing the size of variables on the stack -- also I'm pretty sure fgets() would do something similar to MacGyver's code

    It also means if a line is larger than 10,000 bytes you'll get incorrect results, perhaps the fastest way is (by far not the most efficient):

    Code:
    * seek the end of the file
    * find out how many bytes you are from the start (ie how big the file is)
    * allocate a buffer big enough to hold the file
    * read in the file into the buffer
    * loop through the buffer char-by-char looking for new lines (while keeping a running total)
    * free the buffer

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Well, if you're concerned about stack space, you can always allocate on the heap.
    fgets is also supposed to return a char*, but msdn docs never say what it returns if there's not an error. I'm thinking you can find out whether fgets was able to read an entire line or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You said so yourself that you don't like supporting IE because it's not standards compliant. Hence don't use the MSDN docs for standard C -- they seem to 'miss-label' things (ie POSIX compliant functions), or totally forget to mention things.

    Quote Originally Posted by man fgets (3)
    Upon successful completion, fgets() and gets() return a pointer to the
    string.
    In other words, NULL on failure else the string.

    Quote Originally Posted by man fgets (3)
    The fgets() function reads at most one less than the number of characters
    specified by n from the given stream and stores them in the string s.
    Reading stops when a newline character is found, at end-of-file or error.
    The newline, if any, is retained. If any characters are read and there
    is no error, a `\0' character is appended to end the string.
    Last edited by zacs7; 12-25-2007 at 04:24 AM.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    They suffice well enough. Despite severals flaws, they do describe the functions.
    I wouldn't recommend it 100% to newbies, but it will suffice since it's the only doc I have offline.
    But in case it returns a pointer to the string, then all you have to do is:

    Code:
    int lines = 0;
    char buffer[10000];
    FILE *f;
    
    /* Assume f points to a valid FILE opened for reading... */
    
    while( fgets(buffer, sizeof(Buffer), f) )
    {
    	/* Only increment lines if fgets read an entire line, otherwise continue reading the line */
    	if ( Buffer[ strlen(Buffer) ] == '\n' )
    		lines++;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or...

    Code:
    if(strchr(ret, '\n') != NULL)
        lines++;
    Don't forget, fgets() stops at a newline, so there's never going to be a newline in the string other than at the end

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Indeed, so if there isn't a newline, the entire line was not read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM

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