C ++ - Missing line break character in read file

I use C ++ streams to read into a bunch of files in a directory, and then write them to another directory. Since these files can be of different types, I use the common ios :: binary flag when reading / writing these files. Sample code below:

std::fstream inf( "ex.txt", std::ios::in | std::ios::binary);
char c;
while( inf >> c ) {
    // writing to another file in binary format
}

The problem is that in the case of files containing text, the line breaks in these text files are not written to the output file.

Edit: Or at least they don't look like when a new file is opened, there is only one continuous line of characters.

Change again: the problem (continuous line) persists even when the read / write is in text mode.

So, I was wondering if there is a way to check if the file is in text or binary format, and then read / write it correctly. Else, is there a way to keep the end of line characters even when opening a file in binary format?

Edit: I am using the g ++ 4.8.2 compiler

+4
source share
1 answer

If you want to manipulate bytes, you need to use readand methods write, and not >> <<.

You can get the expected behavior with inp.flags(inp.flags() & ~std::ios_base::skipws);.

+2
source

Source: https://habr.com/ru/post/1614104/


All Articles