C++ change newline from CR+LF to LF

前端 未结 2 1387
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 02:10

I am writing code that runs in Windows and outputs a text file that later becomes the input to a program in Linux. This program behaves incorrectly when given files that hav

2条回答
  •  隐瞒了意图╮
    2020-12-10 02:24

    Yes, you have to open the file in "binary" mode to stop the newline translation.

    How you do it depends on how you are opening the file.

    Using fopen:

    FILE* outfile = fopen( "filename", "wb" );
    

    Using ofstream:

    std::ofstream outfile( "filename", std::ios_base::binary | std::ios_base::out );
    

提交回复
热议问题