|
-
Apr 13th, 2002, 07:10 AM
#1
Thread Starter
Lively Member
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.
-
Apr 13th, 2002, 08:13 AM
#2
Monday Morning Lunatic
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
-
Apr 13th, 2002, 10:04 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|