Tag: fflush

C:清除STDIN

基本上在每个printf之前的windows的代码块我有“fflush(stdin);” 哪个有效。 当我将我的代码复制到Linux时,它不起作用,也没有“fflush(stdin);”的替代方案。 我发现了。 无论我采用哪种方式,输入似乎都没有在缓冲区中清除,或者我的代码中的某些内容不正确。 #include #include #include #include int main() { char pbuffer[10], qbuffer[10], kbuffer[10]; int p=0, q=0, k=0; int r, i, Q, count, sum; char a[3]; a[0]=’y’; while(a[0]==’y’ || a[0]==’Y’) { printf(“Enter ap value: \n”); fgets(pbuffer, sizeof(pbuffer), stdin); p = strtol(pbuffer, (char **)NULL, 10); printf(“Enter aq value: \n”); fgets(qbuffer, sizeof(qbuffer), stdin); q = […]

fflush(stdin)无法在cygwin中使用gcc编译,但使用visual studio 2010进行编译

我(重新)学习编程,我从C开始。我的IDE(如果我可以这么说)是Windows7上的cygwin(32Bit)和Visual-Studio 2010。 我一直在编译用gcc(cygwin)和VS2010编译器编写的代码。 我想,我这样做是因为我认为这是一种很好的学习方式。 无论如何,我刚刚了解了fflush(stdin),即刷新stdin缓冲区。 似乎是一个很好的function,因为使用scanf似乎很痛苦。 所以我根据我的教科书中的一个例子编写了下面的代码。 它在cygwin和VS2010中都使用gcc进行编译。 当我运行VS编译的程序时它工作正常(s.below),当我在cygwin中运行gcc编译的程序时,fflush(stdin)不会刷新stdin缓冲区(s.below)。 我已经读过一些关于fflush(stdin)的线程,在某些情况下有一个未定义的行为。 无论这意味着什么,我都是从C for Linux Programming教科书中获取的。 如果fflush(stdin)不是一个有效的方法来摆脱stdin缓冲区中的东西,还有什么其他标准方法? 非常感谢任何答案! ==在Windows下运行的程序: enter a long integer and a double 23.00 78.99 lint = 23 dt = 0.00 enter a five digits input 78493 u entered 78 and 493 ==程序在Cygwin中运行: enter a long integer and a double 23.00 78.99 lint = […]

c – 需要替代fflush

我只允许使用标准的c库,所以fpurge()不是我的选择。 int dim = 0; int c = 0; printf(“Please enter a number”); while ( (( c = scanf(“%d”,&dim)) != 1 ) || (dim UCHAR_MAX) ) { if (c == EOF) { return 0; } fflush(stdin); printf(“[ERR] Enter a number\n”); printf(“Please enter a number”); } 该程序应该读取一个大于一的数字,如果有任何“错误的”输入,如字母,它应该去并传递错误信息。 它适用于我的Windows PC上的fflush,该程序应该在Linux系统上运行。 如何更换程序仍然有效的fflush? 因为当我不使用它时,我进入了一个无限循环。 输入的数字决定了在程序的其余部分中使用某行代码的频率,这就是我需要数组的原因。

怎么来fflush(stdin)function不起作用?

我的主要问题是为什么fflush(stdin); function不起作用? 每当我运行代码时,我都无法使用空格ex获得第二个输入。 你好世界,但我得到你好? 谢谢 #include main(){ int x; double y; char string[100]; /* * string input */ printf(“Enter one word: “); scanf(“%s”, string); // note there is no & before string */ printf(“The word you entered was >>%s<<\n"); printf("Enter many words: "); fflush(stdin); // >%s<<\n"); getchar(); }

fflush(stdout)in c

当我在fflush(stdout)并且我在GDB中rest时,在我实际打印它之前我能知道stdout中有什么吗? 我怎么能在任何时间点知道stdout中有什么?

fflush(stdin)ANSI C

我是ANSI C的初学者,我有一个问题,这可能是一个愚蠢的问题,我很抱歉。 #include main() { int age; printf(“Hello World!\n”); printf(“Please enter your age: “); scanf(“%d”, &age); printf(“You entered %d\n”, age); fflush(stdin); getchar(); } 这是我学习scanffunction的第二个程序。 我的问题是:我知道printf , scanf , fflush , stdin和getchar是在stdio.h中定义的,但只有当我使用fflush(stdin)时我必须把#include < stdio.h > ,但是当使用任何其他方法时我可以删除该行#include。

这是刷新C输入流的正确方法吗?

好吧,我一直在google上搜索很多关于如何正确刷新输入流的问题。 我所听到的只是关于输入流如何定义fflush()的混合论据,有些人说这样做,而其他人只是说不要这样做,我没有太多运气找到明确的效率/这样做的正确方法,大多数人都同意..我在编程方面很陌生,所以我还不知道该语言的所有语法/技巧,所以我的问题是哪种方式是最有效/最合适的解决方案清除C输入流?? 在尝试接收更多输入之前,请使用getchar()两次? 只需在输入上使用fflush()函数? 要么 这就是我认为我应该这样做的方式。 void clearInputBuf(void); void clearInputBuf(void) { int garbageCollector; while ((garbageCollector = getchar()) != ‘\n’ && garbageCollector != EOF) {} } 所以,每当我需要读取一个新的scanf(),或者使用getchar()来暂停程序时,我只需要调用clearInputBuf ..那么这三种解决方案中最好的方法是什么呢?还是有更好的选择?

了解fflush()的必要性以及与之相关的问题

以下是使用fflush()的示例代码: #include #include #include #include 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 […]

即使使用fflush(stdin),程序也不会在scanf()之后执行gets()

在浪费了太多时间搜索为什么我的程序在使用scanf()之后没有执行gets()时,我找到了一个解决方案,即在scanf()之后使用fflush(stdin)来使gets()获得一个字符串。 问题是fflush(stdin)没有做到预期的结果:程序继续跳过gets(),我不能在控制台中写任何短语来读取。 我的代码是下一个: #include #include int main(){ char nombre[10]; char mensaje[80]; printf(“Type your name:\n”); scanf(“%s”, nombre); fflush(stdin); printf(“Now, type a message:\n”); gets(mensaje); printf(“3/%s:%s”,nombre,mensaje); return 0; }

fflush(stdin)function不起作用

我似乎无法弄清楚这段代码有什么问题: #include #include #include #include #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: “); […]