为什么 printf 创建 windows 行结尾?

Why does printf create windows line endings?

所以我正在尝试复制(稍后修改)一个 .ppm 文件。我在 Windows 10 使用 mingw g++。 原始文件仅为 LF,但使用我的程序创建的文件具有 CRLF,这会破坏 .ppm 文件。我没有在任何地方做 \r\n 但它仍然被输出。

FILE *fp;
FILE *dest;

char magicNumber[3];
int width, height, depth;
unsigned char red, green, blue;
unsigned char* buff;

printf("Hello, World!\n");


fp = fopen("lenna.ppm", "r+");

fscanf(fp, "%s", magicNumber);
fscanf(fp, "%d %d %d", &width, &height, &depth);
printf("%s %d %d %d nums\n", magicNumber, width, height, depth);


dest = fopen("lena2.ppm", "w+");
fprintf(dest, "%s\n%d %d\n%d", magicNumber, width, height, depth);

结果

为什么?

我只想有LF。我该怎么做?

以二进制模式打开文件:

fopen("lena2.ppm", "wb+");

来自docs

In text mode, carriage return-line feed combinations are translated into single line feeds on input, and line feed characters are translated to carriage return-line feed combinations on output.