How can I get a file39s size in C

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Possible Duplicate:
http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c


How can I find out the size of a file? I opened with an application written in C. I would like to know the size, because I want to put the content of the loaded file into a string, which I alloc using malloc(). Just writing malloc(10000*sizeof(char)); is IMHO a bad idea.

Answers

You need to seek to the end of the file and then ask for the position:

fseek(fp, 0L, SEEK_END);
sz = ftell(fp);

You can then seek back, e.g.:

fseek(fp, 0L, SEEK_SET);

or (if seeking to go to the beginning)

rewind(fp);

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils