C++ Line Feed converted to Carriage Return & Line Feed

71 views Asked by At

I'm in the process of writing a file that has to save numeric data as chars to be read by a different, already-developed program. It has been simple enough in execution so far, but I'm having trouble storing the numbers 10, which is the line feed character. When attempting to write a char of values 10 into a file, it is automatically converted in to the two separate characters of 13 and 10, Carriage Return and Line Feed.

For example...

std::ofstream outputFile(newFileLoc.bin, std::ofstream::out);
std::string newLineNonsense = { 10, 10, 10, 13, 13, 13 };
outputFile << newLineNonsense;
outputFile.close();

(Note: newFileLoc.bin is a full file path in the actual code)

opening up newFileLoc.txt with Notepad++ reveals that what should be lf lf lf cr cr cr is instead crlf crlf crlf cr cr cr. And to confirm this wasnt Notepad++ making modifications, I checked the file size beforehand. A 6 character text file should be 6 bytes, but newFileLoc.bin was 9 bytes in size even before opening the file.

For some compilation information, I'm using Visual Studio 2022 and C++17 as the language standard, and the unicode character set. Changing to Multi-byte did not help.

I'm aware this system is archaic at best, but I'm working within the bounds of pre-developed software. Is there a way to disable the windows-ification of the line feed character? Is this an fstream issue or a string issue?

0

There are 0 answers