-
File size in bytes
I was wondering if there is a more efficent way to calculate actual size in bytes of any file,
here is how I am doing.
win32 API which will do it for me. accuracy is important as I will be reading the entire file in to a shared buffer.
Code:
fstream reader("myfile",std:ios:binary| std::ios::in);
unsigned int counter =0;
unsigned char myReader;
while(!reader.eof)
{
reader.read((char *)&myReader,sizeof(unsigned char));
counter++
}
reader.close();
unsigned int filesize = counter;
I am hoping how many times the loops run will tell me the actual size in bytes. Once I know what the size is then I can allocate memory and read the entire file all at once in to the shared buffer.
-
Re: File size in bytes
Did you try _filelength, _filelengthi64?
-
Re: File size in bytes
Code:
#include <stat/sys>
struct stat stbuf;
stat("/tmp/file.txt", &stbuf);
size_t size = stbuf.st_size;
-
Re: File size in bytes
-
Re: File size in bytes
Since you mentioned the Win32 API you might consider GetFileSize or GetFileSizeEx.
-
Re: File size in bytes