QQ网名大全

c语言的函数_c语言函数的定义和声明

fflush(stdin) ;
清空输入缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符,此时应该先执行fflush(stdin);)
详细解释一下:
/*stdin就是标准输入 std即standard,in即输入,合起来就是标准输入。 一般就是指键盘输入到缓冲区里的东西。 */
函数名: fflush
功 能: 清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
原型:int fflush(FILE *stream)
程序例:
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* create a file */
stream = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush\
DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without\
closing it */
flush(stream);
printf("\nFile was flushed, Press any key\
to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush the stream's internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush\
the DOS buffer */
close(duphandle);
}
fflush 的返回值类型是int类型,那么这个int类型具体的返回是什么呢?
返回值:
如果成功刷新,fflush返回0。指定的流没有缓冲区或者只读打开时也返回0值。返回EOF指出一个错误。
注意:如果fflush返回EOF,数据可能由于写错误已经丢失。当设置一个重要错误处理器时,最安全的是用setvbuf函数关闭缓冲或者使用低级I/0例程,如open、close和write来代替流I/O函数。
#include <stdio.h>
int main()
{
int i=0;
char p;
scanf("%d",&i);
问题点---> fflush(stdin);
printf("I is :%d\n",i);
scanf("%c",&p);
printf("Char P is %c.\n",p);
return 0;
}
该程序在dev-c++下编译执行后
2 <enter>
I is :2
a <enter>
Char P is a.
在linux下编译执行后
2 <enter>
I is :2
Char P is
.
很明显fflush在linux下没有起到任何作用?请问我错在哪里了?我要怎样清流?当然在linux下我把fflush(stdin);换成getchar();可以达到效果
佚名
2024-05-23 11:13:43
最佳回答
类似问题(10)
  • 佚名
    2024-05-23 10:21:28

    C语言函数

    INT Getchar简单的说就是你自己取得函数名字 函数名只能以字母,下划线,数字组成,而且只能以下划线或字母为开头

  • 佚名
    2024-05-23 05:38:33

    c语言--函数

    你的函数声明写错了 应该是 double f(int n);for(i=0;i<m;i++) int i; double s=1....

  • 佚名
    2024-05-23 21:10:18

    C语言的函数

    你不是说那是无参函数吗?怎么从主函数传参?

  • 佚名
    2024-05-23 14:56:33

    c语言函数

    //测试已通过,望采纳#include<stdio.h> #define max 10void input(int arr[]){//输入 int i; ...

  • 佚名
    2024-05-23 06:17:51

    C语言 函数

    C语言中一个函数(function)是一个可以从程序其它地方调用执行的语句块。 1、通过使用函数(functions)我们可以把我们的程序以更模块化的形式...

  • 佚名
    2024-05-23 11:15:10

    c语言的函数

    实参

  • 佚名
    2024-05-23 20:22:40

    C语言函数

    要求:不使用判断、跳转、循环;按照这个要求的话楼上两位兄弟的不符合,我尝试了一下方法需要用到头文件#include <cstdlib> int fun(i...

  • 佚名
    2024-05-23 08:37:10

    C语言 函数设计

    //参考# include <stdio.h>int sumAB(int a, int b){    int i, sum = 0;    if (a >...

  • 佚名
    2024-05-23 03:59:40

    C语言函数执行

    前面给定一个接收字符的变量,比如c,switch(c){case 'a':A;break;case 'b':B;break;...}

  • 佚名
    2024-05-23 05:39:13

    C语言(用函数)

    #include<stdio.h>#include<string.h>void print(int n){//输入n,打印1-n的乘法表 int ...