fflush

关于printf()与fflush()【转】

北慕城南 提交于 2019-11-26 18:33:37
原文: http://www.cnblogs.com/ymy124/archive/2012/02/13/2349299.html 用printf()输出时是先输出到缓冲区,然后再从缓冲区送到屏幕上。那什么情况下才会将缓冲区里的内容送到屏幕上呢?一,使用fflush(stdout)强制刷新。二,缓冲区已满。三,scanf()要在缓冲区里取数据时会先将缓冲区刷新。四,\n,\r进入缓冲区时。五,线程结束的时候,如果该线程里也有printf(....);六,程序结束时。 #include<stdio.h> #include<stdlib.h> int main() { int i=89; int x; printf("--------------\n");//输出-----------后不会立即输出89; printf("%d",i);//在这条语句后加上fflush(stdout);则会立即输出89 sleep(5); scanf("%d",&x);//5秒后,遇到scanf时才将89输出到屏幕上。 return 0; } 另外,fclose函数关闭文件时,该函数会将保存在内存中尚未来得及写回磁盘的文件内容写到磁盘上,如果没有调用fclose函数,就必须等待内存中缓冲区被填满,由系统将其内容写回到磁盘上去。 转载于:https://www.cnblogs.com/Leo-Forest

How is the working of fflush(stdin) changing the output in below code?

戏子无情 提交于 2019-11-26 12:33:31
#include <stdio.h> int main() { int test_no ,count=1,i,n,j; scanf("%d",&test_no); fflush(stdin); int arr1[test_no]; for(i=0;i<test_no;i++) { scanf("%d",&n); printf("\n"); int arr[n]; for(j=0;j<n;j++) { fflush(stdin); scanf("%d",&arr[i]); } for(j=1;j<=n-1;j++) { if(arr[j-1]>arr[j]) { count++; } } if(n==1) { arr1[i]=1; } else { arr1[i]=count; } count=1; } for(i=0;i<test_no;i++) { printf("%d\n",arr1[i]) ; } return 0; } This solution is to this problem . I am not getting the desired output for the third case , it's giving me output as 3 or 4 depending on whether I place fflush(stdin) before scanf(

fflush(stdin) function does not work

旧巷老猫 提交于 2019-11-26 08:37:51
问题 I can\'t seem to figure out what\'s wrong with this code: #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAX 100 #define TRUE 1 #define FALSE 0 char sect_cat; char customer_name[MAX]; char customer_number[MAX]; /* error handling is easier */ int prev_unit = 0; int current_unit = 0; int consumed = 0; int set = FALSE; float init_bill; float tax; float total_bill; void get_userinfo() { printf(\"Enter sector category: \"); scanf(\"%c\", &sect_cat); printf(\

How is the working of fflush(stdin) changing the output in below code?

半城伤御伤魂 提交于 2019-11-26 02:59:39
问题 #include <stdio.h> int main() { int test_no ,count=1,i,n,j; scanf(\"%d\",&test_no); fflush(stdin); int arr1[test_no]; for(i=0;i<test_no;i++) { scanf(\"%d\",&n); printf(\"\\n\"); int arr[n]; for(j=0;j<n;j++) { fflush(stdin); scanf(\"%d\",&arr[i]); } for(j=1;j<=n-1;j++) { if(arr[j-1]>arr[j]) { count++; } } if(n==1) { arr1[i]=1; } else { arr1[i]=count; } count=1; } for(i=0;i<test_no;i++) { printf(\"%d\\n\",arr1[i]) ; } return 0; } This solution is to this problem. I am not getting the desired

I am not able to flush stdin

人盡茶涼 提交于 2019-11-26 00:22:49
问题 How to flush the stdin ?? Why is it not working in the following code snippet? #include <string.h> #include <stdio.h> #include <malloc.h> #include <fcntl.h> int main() { int i=0,j=0, sat; char arg[256]; char * argq; argq = malloc(sizeof(char)*10); printf(\"Input the line\\n\"); i=read(0, arg, sizeof(char)*9); arg[i-1]=\'\\0\'; fflush(stdin); i=read(0, argq, sizeof(char)*5); argq[i-1]=\'\\0\'; puts(arg); puts(argq); return 0; } Now if i give the input as 11 characters, only 9 should be read

Using fflush(stdin)

眉间皱痕 提交于 2019-11-25 21:35:17
问题 So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that\'s exactly how my CS professor taught the class to do it. How bad is using fflush(stdin) ? Should I really abstain from using it, even though my professor is using it and it seems to work flawlessly? 回答1: Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard: int fflush(FILE