LEN of a file
What is the best way to determine the size of a file? I've opened it for read access using fopen(), and would like to know the size of the file before I proceed further. I checked MSDN and found a list of stream i/o functions, but didn't see anything for this. I suppose that I could do something like this
But that isn't working. I also tried
cout<<"Reading data from input file..."<<endl;
while(fread(temp, 1, 1, input))
But for some reason, that always gave me 0, even though fread(), according to MSDN, "returns the number of full items actually read, which may be less than countif an error occurs or if the end of the file is encountered before reaching count.
len=fread(temp, 1, 100000000, input);
So, how do you determine the size of a file in bytes? Thanks.
I think fseek and fgetpos are probably your best bet. Just seek to the end, and then get the position.
Edit: I don't use these functions much, but I think this'll work (might want some error checking in there, but anyways...)
fseek(input, 0, SEEK_END);
>> The second problem is that SEEK_END with fseek() is ``undefined'' for text files.
Out of curiosity, why is that?
This should do the trick.
if ( file.is_open() )
file.seekg (0, ios::end);
int len = file.tellg();
cout << "The file is " << len << " bytes." << endl;
cout << "Error opning file." << endl;