Results 1 to 3 of 3

Thread: ansi c: length of file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117

    Post ansi c: length of file

    Is there any way to find out the length of a txt file ( number of lines)?

    I need to be able to find this out in order to poplate an array with an unspecified amount of data from a txt file.


  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can find out its byte size, but not the number of lines without reading it all in. This is because text files are just binary files interpreted differently, where each line ends with a newline character '\n'.

    So, you can read it through and count the lines, or just read each line into a buffer that should be long enough, say 2048 bytes, then use a dynamic array of some description.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    jim mcnamara
    Guest
    stat() or _stat() is available in all c implementations.

    Code:
    #include <sys/stat.h>
    
    int fileLen(char *path){
       //in VC++ use the _stat() function
    struct _stat buffer;
    
    int i = _stat( path,  &buffer );
    return buffer.st_size;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width