使用system()函数时混淆控制台和文件输出

ami*_*n__ 1 c file-io system

我有以下代码,

#include<stdio.h>
#include<stdlib.h>

int main()
{
    //freopen("file1","w",stdout);
    char str[]="command line with file";
    char s[]="extra string";
    puts(str);
    puts(s);
    system("PAUSE");    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在控制台上看到输出时它显示了我,

command line with file
extra string
Press any key to continue . . . 
Run Code Online (Sandbox Code Playgroud)

我通过删除代码中的注释行将输出写入文件时期望相同​​的输出.但它输出像,

Press any key to continue . . . 
command line with file
extra string
Run Code Online (Sandbox Code Playgroud)

为什么文件和控制台之间的区别输出??? 这里system("PAUSE")函数负责字符串输出Press any key to continue . . .

Ala*_*rry 6

写入终端时,stdout是行缓冲的.它立即写入每一行.写入文件时,它是块缓冲的.它在缓冲区中存储几千字节,并在您调用fflush时或缓冲区已满或程序结束时将其刷新.暂停消息由一个单独的进程写入,该进程在原始进程之前退出,此时它必须刷新其缓冲区(如果有的话).然后你的原始进程system()结束并到达结束main()并退出,刷新包含两个测试字符串的缓冲区.