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 - ??
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.
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.
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.
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.