[问题] 有关于 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