Skip to content

Should ifstream be opened in binary mode? #15

@lightvector

Description

@lightvector

It appears that zstr does not open input streams in binary mode (std::ios_base::binary):

explicit ifstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
    : detail::strict_fstream_holder< strict_fstream::ifstream >(filename, mode),
      std::istream(new istreambuf(_fs.rdbuf()))

Although it does for the output stream:

explicit ofstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
    : detail::strict_fstream_holder< strict_fstream::ofstream >(filename, mode | std::ios_base::binary),
      std::ostream(new ostreambuf(_fs.rdbuf()))

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions