簡體   English   中英

使用system()函數時混淆控制台和文件輸出

[英]confusing console and file output when using system() function

我有以下代碼,

#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;
}

當我在控制台上看到輸出時,它顯示了我,

command line with file
extra string
Press any key to continue . . . 

通過刪除代碼中的注釋行,將輸出寫入文件時,我希望得到相同的輸出。 但是它輸出像

Press any key to continue . . . 
command line with file
extra string

為什么文件和控制台輸出之間存在這種區別??? 這里的system(“ PAUSE”)函數負責字符串輸出。 Press any key to continue . . . Press any key to continue . . .

寫入終端時, stdout是行緩沖的。 它立即寫入每一行。 寫入文件時,它是塊緩沖的。 它在緩沖區中存儲了幾千字節,並在調用fflush或緩沖區已滿或程序結束時將其清除。 暫停消息是由一個單獨的進程編寫的,該進程在原始進程之前退出,此時必須刷新其緩沖區(如果有的話)。 然后,原始進程的system()結束,並到達main()的末尾並退出,刷新包含兩個測試字符串的緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM