Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Length of a file in C
Name: Mr. A Date: February 22, 2002 at 02:11:05 Pacific
Comment:
I'm running FreeBSD 3.4-RELEASE, GNU 2.72 C/C++ compiler. I want to know what function returns the length of a file. I could read each byte until I reach the end, but there's got to be a function. I've read so many manpages and looked through header files and I can't find it. I prefer doing it with a file descriptor rather than assocating it with a stream.
Name: ananth Date: February 22, 2002 at 09:41:06 Pacific
Reply:
Hi Do something that looks like this. lseek would return what you want.
Ananth.
- #include #include
int main() { int f1;
/* get the file size */ f1=open("filename",O_RDONLY); printf("%d;\n",lseek(f1,0,SEEK_END)); close(f1); }
--
0
Response Number 2
Name: Mr. A Date: February 22, 2002 at 12:31:33 Pacific
Reply:
Ananth, Thank you. That will do it. I was hoping that there was a function like the Borland getfilelength(). FYI: Using fseek is a little different. I spent many hours one day going crazy and I thought I had found a bug in my compiler. I found out that when reading & writing to a file when you are using fseek(), in between subsequent reads & writes, you have to perform fseek(), even if it is fseek(somefile, 0, 1). Then I read the explanation in The C Programming language. It's to be backwards compatible with older programs that used fseek(). That book could have saved me about 12 hours of poking around and major frustration.
Summary: Ok here is the deal: subject: Writing data to a file in C problem: I need to be able to write the data as the program is running not just when I close the file. example: Im running a packet sniffer...
Summary: I can't figure out how to get the length of a file in Python. The files I'm dealing with are relatively small, so I could read() the whole thing into a buffer then get len(buffer), but there must be ...
Summary: Sorry for the double posting but i needed to change the subject.... Im trying to make a function in c that returns a file pointer to the main program so that i can manipulate the file further. is this...