[問題] 有關於 fflush 和 setvbuf

看板C_and_CPP作者 (人生在世很愜意)時間8年前 (2016/02/04 14:12), 編輯推噓3(306)
留言9則, 5人參與, 最新討論串1/1
開發平台(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有何功用? -- 志願役普遍垃圾不代表每個志願役都是垃圾。 苗栗人智商普遍低落,不代表每個苗栗人智商都很低。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.30.118 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1454566330.A.006.html

02/04 17:15, , 1F
你用什麼OS和Compiler和c lib? 這似乎是你的平台才有的問題
02/04 17:15, 1F

02/04 17:17, , 3F
我在上面網站測是沒問題的,在linux Mint17也沒問題
02/04 17:17, 3F

02/04 18:05, , 4F
忘記寫了 dev C++
02/04 18:05, 4F

02/04 18:10, , 5F
看起來是Windows系統,那我就懶得測了..
02/04 18:10, 5F

02/04 18:11, , 6F
至少在linux上,sample code是正常的
02/04 18:11, 6F

02/06 15:52, , 7F
fflush 好像是linux才在用的..
02/06 15:52, 7F

02/06 23:04, , 8F
@Qoofate , Windows 表示 : 其實我也會用 ...
02/06 23:04, 8F

02/06 23:19, , 9F
那兩行在結束的時候應該會印出 只是cmd被關掉了沒看到吧
02/06 23:19, 9F
文章代碼(AID): #1Mikkw06 (C_and_CPP)