tags:

views:

633

answers:

4

While writing a file using ofstream, how do I know when the file's size has reached the OS' maximum file size - more specifically linux's maximum file size - ??

A: 

I think (not 100% sure) that you'd just have to compare the stream's current size after a write to whatever the OS's max file size is. Otherwise I'm guessing the underlying implementation will just let you keep writing until the actual OS io calls fail.

Jim Crafton
+3  A: 

First off, maximum file size is a filesystem limit, not an Operating System limit. It will even vary for a particular filesystem, based on how the filesystem was formatted.

As for how you'd figure out that you'd reached the limit, your code will likely throw an exception when that happens, which you'll then be able to relate back to the OS error codes.

Note that the actual limit is pretty darn big for a "typical" EXT2 filesystem - in the Terabytes. You'll likely never reach it, in practice. If you seriously are accumulating Terabytes of data, you might want to consider whether there's a more reasonable way to store it, rather than a single gigantic file.

Mark Bessey
I think it's a filesystem limit defined ALSO by some kernel parameters. From your link: "The reason for some limits of the ext2-file system are ... and the operating system's kernel" I'm running linux and I can't write more than 2GB to a file. And I have a lot of space left.
Salsa
Its also defined by the compiler and libaries you are using. To write more than 2GB, you need 64 bit file pointers, which may require special compiler flags, even if the underlying system supports it.
KeithB
+2  A: 

You can check if the bad bit is set. Also, using exceptions you can force the stream to throw an exception, when the bad bit gets set.

Kasprzol
Out of curiosity, does this in fact get set in a case like this? Is this implementation dependent or does the standard explicitly say that fstream implementations need to trap for this?
Jim Crafton
If you try to write to a stream and the write operation fails for any reason (including trying to write a file larger than filesystem limit) then the bad bit (or fail bit) will be set.
Kasprzol
A: 

Well, fstream does not have an exclusive exception for a "write to a file that exceeds the implementation-defined maximum file-size" (from 'man 2 write'), like the error code EFBIG available when using the C function 'write'. So, I think one has to do like Jim Crafton said and compare the file size against some user-defined maximum size or against the maximum value held by a 'streamoff' variable, which is the variable-type used to handle file sizes - file offset actually - in iostream.

Salsa