我有以下程序:
int main(int argc, char *argv[])
{
char ch1, ch2;
printf("Input the first character:"); // Line 1
scanf("%c", &ch1);
printf("Input the second character:"); // Line 2
ch2 = getchar();
printf("ch1=%c, ASCII code = %d\n", ch1, ch1);
printf("ch2=%c, ASCII code = %d\n", ch2, ch2);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如上面代码的作者所解释的那样:程序将无法正常工作,因为在第1行,当用户按下Enter时,它将在输入缓冲区中留下2个字符:Enter key (ASCII code 13)和\n (ASCII code 10).因此,在第2行,它将读取\n并且不会等待用户输入字符.
好的,我明白了.但我的第一个问题是:为什么第二个getchar()(ch2 = getchar();)不读取Enter key (13)而不是\n字符?
接下来,作者提出了两种解决此类问题的方法:
使用 fflush()
写一个这样的函数:
void
clear …Run Code Online (Sandbox Code Playgroud)我无法在这里刷新stdin,有没有办法刷新stdin?如果没有,那么如何让getchar()从用户输入一个字符作为输入,而不是输入缓冲区中scanf留下的"\n"? ?
#include "stdio.h"
#include "stdlib.h"
int main(int argc,char*argv[]) {
FILE *fp;
char another='y';
struct emp {
char name[40];
int age;
float bs;
};
struct emp e;
if(argc!=2) {
printf("please write 1 target file name\n");
}
fp=fopen(argv[1],"wb");
if(fp==NULL) {
puts("cannot open file");
exit(1);
}
while(another=='y') {
printf("\nEnter name,age and basic salary");
scanf("%s %d %f",e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add another record (Y/N)");
fflush(stdin);
another=getchar();
}
fclose(fp);
return 0;
}
编辑: - 更新的代码,仍然无法正常工作
#include "stdio.h"
#include "stdlib.h"
int main(int argc,char*argv[]) {
FILE *fp;
char … 每当我在fgets之前执行scanf时,fgets指令就会被跳过.我已经在C++中解决了这个问题,我记得我必须有一些可以清除stdin缓冲区或类似内容的指令.我想C有一个等价物.它是什么?
谢谢.
我有一个问题,经过多次测试,我认为这是由于我不理解输入缓冲区如何工作.
我有一个while循环,它应该继续迭代,直到用户输入"no"来停止迭代.
我有两个问题.
码:
int foo = 0;
do{
int i, cycles;
char array[MAX_LENGTH+1];
for(cycles=0; cycles < MAX_READ_CYCLES; cycles++){
i=0;
printf("\n\nEnter a string: ");
char ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
array[i] = ch;
i++;
}
array[i] = '\0'; //string terminator
printf("String you entered: %s\n", array);
printf("\nDo you want to continue? 1: yes / 0: no \n");
scanf("%d", &foo);
}
} while( foo == 1);
Run Code Online (Sandbox Code Playgroud)
OUTPUT
Enter a string: test
String you …Run Code Online (Sandbox Code Playgroud) 当我尝试编译我的代码时,我得到以下错误:
error #2168: Operands of '=' have incompatible types 'char [255]' and 'char *'.
error #2088: Lvalue required.
Run Code Online (Sandbox Code Playgroud)
我得到这些错误的同一行(即1044),并在多行,所以我通过修复一个我可以修复其他人,所以让我复制你的代码.你可以跳过并且只读取注释以**开头的行只是为了让它更容易:)并以< - 结束我希望代码注释为你提供良好的服务:首先让我从定义类型PRINTOPT开始
typedef struct {
//UsePl signifies if the user would like to see the graphs without having to export data
//Thanks to PlPlot library.
int usePl;
//Feel free to customize and add to this struct
//for any simulation program you create.
//---- Note2self: probably change the graph bool to an array,
//as later you will have to print around 20 …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,它首先从unix管道接收数据,然后提示用户输入.我似乎无法弄清楚为什么在提示用户输入之前管道的输入似乎没有正确关闭.感觉我在这里缺少一些非常基本的东西.
我曾尝试提出了冲洗标准输入所有的例子在这里没有成功.这也似乎有可能相关,但我还没有设法提取任何相关的答案.
#include <stdio.h>
#include <stdlib.h>
#define BUFSZ 1024
int main(void) {
char *buf = calloc(BUFSZ, sizeof(char));
while(fgets(buf, BUFSZ, stdin)) { printf("Pipe input: %s", buf); }
printf("Enter input: ");
fgets(buf, BUFSZ, stdin);
printf("User input: %s", buf);
free(buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用法和输出示例:
$ cat testdata2 | ./a.out
Pipe input: testdata testdata
Pipe input: testdata testdata2
Pipe input: testdata testdata3
Pipe input: testdata testdata4
Pipe input: testdata testdata5
Pipe input: testdata testdata6
Pipe input: testdata testdata7
Enter input: …Run Code Online (Sandbox Code Playgroud)