Reading files over 4gb

Im using this to get the size of a file:
long long Get_Size(string filename)
	//size_t fileSize = 0;
	long long fileSize = 0;
	ifstream infile(filename);
		infile.seekg(0, ios::end); // move to end of file
		fileSize = infile.tellg();
	cout << "file size is " << fileSize << "\n";
	return fileSize;

Problem is, if the fileSize is larger than 4gb, the size seems to.. wrap.. :)
so a 5 gb file, would return 1gb because 4gb is the maxiumum.
I'm not asking to increase the limit of my programs ability to load large files, but can i make the program check if the file is larger than the maximum?
Or check how many times that the program wraps.

Basically i just want to limit my program to reading 4GB files, but i cant do this because if the filesize is larger than 4gb, it just wraps back to 0 :/
Last edited on
read from file. check the size. if it's 4gb save and then continue reading.
I think a more suffice answer would be to seek to the position of 4GB - 1 (very last byte in the largest file available) and checking to see if you actually moved there. If the seek completed successfully, then the file is 4GB or larger. At that point, you should save the current size and continue reading.
In cases like this, it's much easier to either:
1. Not even bother handling it. Just put in the documentation "don't use with files larger than 4 GiB". It sounds cheap, but it's perfectly legitimate. A program doesn't need to be prepared to handle every possible situation, if it's unreasonable to do so.
2. Use a different library for file handling, since the standard one is stupid.
Last edited on
NGen, could you give me an example of some code that seeks to 4gb then checks if its valid.
infile.seekg(0, 4000000000);//Would this work?? 

thanks helios, this program is basically one im making to learn more about C++, so i'm not going to use any libraries, and im going to try to put a file limit it :P
Topic archived. No new replies allowed.