|
-
January 28th, 2011, 11:18 AM
#1
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.
-
January 28th, 2011, 11:33 AM
#2
Re: File size in bytes
Did you try _filelength, _filelengthi64?
Victor Nijegorodov
-
January 28th, 2011, 11:38 AM
#3
Re: File size in bytes
Code:
#include <stat/sys>
struct stat stbuf;
stat("/tmp/file.txt", &stbuf);
size_t size = stbuf.st_size;
-
January 28th, 2011, 11:44 AM
#4
-
January 31st, 2011, 05:18 AM
#5
Re: File size in bytes
Since you mentioned the Win32 API you might consider GetFileSize or GetFileSizeEx.
- Guido
-
January 31st, 2011, 07:20 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|