我想在 Windows 上为 UNIX 生成一个文件(脚本)。所以我只需要输出 LF
字符,而不输出 CR
字符。
当我执行 fprintf(fpout, "some text\n");
时,字符 \n
会自动替换为 \r\n
在文件中。
有没有办法专门输出\n
(LF
)字符?
语言是C++,但I/O函数来自C。
最佳答案
你可以用二进制模式打开文件,例如
FILE *fpout = fopen("unixfile.txt", "wb");
fprintf(fpout, "some text\n"); // no \r inserted before \n
因此,您传递给 fprintf
的每个字节都被解释为一个字节而没有其他内容,这应该省略从 \n
到 \r 的转换\n
.
来自 std::fopen 上的 cppreference :
File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A'.
关于c++ - C/C++ 打印自定义 EOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481333/