Computing.Net > Forums > Programming > Length of a file in C

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

Reply to Message Icon

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.

Thanks to anybody who can help me. Mr. A




Sponsored Link
Ads by Google

Response Number 1
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.

Thanks again,
Mr. A



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Length of a file in C

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

Determine length of file in Python www.computing.net/answers/programming/determine-length-of-file-in-python/11903.html

make a funct that open a file in C www.computing.net/answers/programming/make-a-funct-that-open-a-file-in-c/9599.html