how C output LF to stdout without being changed to CR LF?

前端 未结 3 718
后悔当初
后悔当初 2020-12-11 05:15

On Windows this

#include 

int main() { 
    putc(\'A\',stdout);
    putc(\'\\r\',stdout); 
    putc(\'\\n\',stdout);
}

outp

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 05:52

    A text file converts the C character '\n' into the native line ending on output, and converts the native line ending on input into a single '\n'.

    To get the result you require, you'd have to change stdout into a binary file stream.

    A partial answer is found here. If you have a C99-compliant library, using:

    if (freopen(0, "wb", stdout) == 0)
        ...oops...operation failed...
    

    will attempt to change standard output to a binary stream. However, on Windows, the 'C99-compliant library' might be a problem. Nominally, this is the portable (because standard) answer. There is likely a Windows-specific function to do the same job.

提交回复
热议问题