[問題] 有關於 fflush 和 setvbuf

作者: apologize (人生在世很愜意)   2016-02-04 14:12:06
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
stdio.h
string.h
問題(Question):
實際測試和原本有出入
餵入的資料(Input):
預期的正確結果(Expected Output):
http://www.tutorialspoint.com/c_standard_library/c_function_setvbuf.htm
如這篇
錯誤結果(Wrong Output):
Going to set full buffering on
This is tutorialspoint.com
This output will g into buff
請按任意鍵繼續 . . .
少了兩行
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
char buff[1024];
memset(buff, '\0', sizeof (buff));
fprintf (stdout, "Going to set full buffering on\n");
setvbuf (stdout, buff, _IOFBF, 1024);
fprintf (stdout, "This is tutorialspoint.com\n");
fprintf (stdout, "This output will g into buff\n");
fflush (stdout);
fprintf (stdout, "and this will appear when programm\n");
fprintf (stdout, "will come after sleeping 5 seconds\n");
sleep (5000);
system("PAUSE");
return 0;
}
補充說明(Supplement):
參閱這網站:http://blog.xuite.net/mb1016.flying/linux/28543400-fflush(stdout)
多少理解fflush的意思,可是我自己測試跟實際有點出入。
為什麼我fflush 之後就被截掉了? 我印象中他展示是沒有被截掉。
還有設定 setvbuf有何功用?
作者: qsort (Cos)   2016-02-04 17:15:00
你用什麼OS和Compiler和c lib? 這似乎是你的平台才有的問題http://www.tutorialspoint.com/compile_c_online.php我在上面網站測是沒問題的,在linux Mint17也沒問題
作者: apologize (人生在世很愜意)   2016-02-04 18:05:00
忘記寫了 dev C++
作者: qsort (Cos)   2016-02-04 18:10:00
看起來是Windows系統,那我就懶得測了..至少在linux上,sample code是正常的
作者: Qoofate (大魯閣_最強打者)   2016-02-06 15:52:00
fflush 好像是linux才在用的..
作者: EdisonX (卡卡獸)   2016-02-06 23:04:00
@Qoofate , Windows 表示 : 其實我也會用 ...
作者: kaomark   2016-02-06 23:19:00
那兩行在結束的時候應該會印出 只是cmd被關掉了沒看到吧

Links booklink

Contact Us: admin [ a t ] ucptt.com