-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should ifstream be opened in binary mode? #15
Comments
Hm, the issue that I see there is that if the input data is not in fact compressed, the stream might not work as expected, because it then is supposed to act like a normal istream, with the end-of-line conversions. |
Ah, glad to see a comment/reply. :) Is there any way to get the best of both worlds? If not, then it seems like having it fail to do end-of-line conversions when reading a file that happens to not be compressed seems like the lesser issue. Because as it stands, on Windows the library typically fails to do it's job at all - be able to read compressed files, which is the whole point of using it. |
Makes sense. In that case, a simple workaround would be to have a little helper function that first opens the file and tries to find the magic bytes. If found, continue to open the file with bool is_gzip_compressed_file( std::string const& file_name )
{
// Open the file in binary mode.
std::ifstream infile;
infile.open( file_name, std::ifstream::in | std::ifstream::binary );
if( !infile.good() ) {
return false;
}
// Get the first two characters. If this fails, the file is too short, so it is not a gzip file.
unsigned char buffer[2];
infile.read( reinterpret_cast<char*>( &buffer ), 2 );
if( !infile.good() ) {
return false;
}
infile.close();
// Check if the file starts with the magic number of gz files.
return magic = ( buffer[0] == 0x1f ) && ( buffer[1] == 0x8b );
} That currently only does gzip, but can easily be extended to check the zlib magic bytes as well. |
I'd say binary mode is the best default, I don't know in what case you would want something that probably is compressed to automatically get mangled line endings? I'd say it's more surprising that the data is suddenly mangled, people that want the line ending stuff usually know about it and can pass the required flags. |
It appears that zstr does not open input streams in binary mode (
std::ios_base::binary
):Although it does for the output stream:
This might be a bug? When running on Linux, I haven't found any issues, but when compiling and running on Windows, non-binary mode appears to corrupt the bytes read in, presumably because in text mode it attempts to do some Windows-specific end-of-line character conversions. Adding
std::ios_base::binary
to the ifstream mode fixes this.The text was updated successfully, but these errors were encountered: