Advertisement

Getting the size of a file

Started by August 23, 2000 08:28 AM
5 comments, last by Floru 24 years, 4 months ago
Hi! I have a very stupid question... How do I get the size of a file. In C and in C++? Thanks Floru
Use fseek to move the file pointer to the end and then do an ftell to get the size. (Or does fseek also return the new position??)

Tim
Advertisement
If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
This seems to come up every other week.

stat() returns a bunch of info about a given file. Including the size.

-Mike
ditto... cut from my code:

curpos = ftell(f);
fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, curpos, SEEK_SET);
Chris Brodie
You could also do this:

    #include <io.h>// ...FILE *input;// blablalong fileSize = _filelength(_fileno(input));    


Why they put underscores in front of the names is beyond me. Perhaps because these functions aren''t ANSI C (you won''t find them under UN*X).

Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
Advertisement
If you wanna risk using MFC you could use CFile -

    CFile file;int sizeoffile;if (file.Open("myfile.txt", CFile::typeBinary | CFile::modeRead, NULL)) // open the file, in binary and read modes{sizeoffile = file.GetLength();	// get the size of the filefile.Close();  // close the file}    


Read up on it on MSDN if you wanna know more.. this is of course presuming you are using Visual C++
jumble-----------

This topic is closed to new replies.

Advertisement