Advertisement

Getting File Size

Started by December 30, 2000 05:28 PM
4 comments, last by Galileo430 24 years ago
Does anyone know a fast way of getting a file''s size? I suppose it could be done by reading 1 byte at a time and waiting for fread to stop reading.. Anyone know a better way?
------------------------------------------------------------I wrote the best video game ever, then I woke up...
FILE *fp = fopen("FileName","r"); // Open the file

fseek(fp,0,SEEK_END); // Go to end
fgetpos(fp,&pos); // Get position
printf("#Bytes = %ld",pos); // Output length
fseek(fp,0,SEEK_SET); // Go back to the beginning if you want

LostLogicwww.GamerOutfit.comXBox 360 Community Games Reviews and NewsExisled - 2D/3D Shooter for XBox 360

Advertisement
Use filelength().

  int handle;int length;handle = open("myfile.txt", O_CREAT);length = filelength(handle);close(handle);  


When life hands you lemons, throw them at God''s head.
The Win32 way:
HANDLE file;DWORD FileSize;file = CreateFile("file.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);FileSize = GetFileSize(file, (LPDWORD)NULL);CloseHandle(file); 



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
The real(tm) way

savepos = ftell(somefile);
fseek(somefile, 0, SEEK_END);
fsize = ftell(somefile);
fseek(somefile, savepos, SEEK_SET);
return(fsize);

-Mezz
The way that Mezz gives is IMO the best becuase it uses ANSI functions. filelength isn''t supported on all systems, etc....

This topic is closed to new replies.

Advertisement