Tag: scanf

使用scanf扫描一行中的多个输入

我正在尝试扫描单行输入并将其存储在结构中。 我不确定我是否存储错误或者我打错了。 我不确定如何将scanf与for循环一起使用,因为我从来没有这样做过,更不用说C喜欢覆盖东西了。 所以我不确定如何处理这个问题。 这是我放在一起的东西,但是当我打印时,我得到了垃圾数字。 我本打算使用指针,但我的教授不会让我们使用它。 这就是我遇到麻烦的原因。 EDITED #include #include #include #define MAX 3 #define MAXTRIP 6 struct stop { float cost; float time; }; struct trip { char Dest_letter; int stop_num; struct stop leg[MAX]; }; int main(void) { int trip_num, index, i; struct trip travel[MAXTRIP]; printf(“Enter number of trips: “); scanf(“%d”, &trip_num); printf(“Please enter […]

如何在C中使用EOF stdin

我需要将坐标输入到数组中,直到遇到EOF,但我的代码中出现了错误。 我用ctrl + Z,ctrl + D. int main() { int x[1000],y[1000]; int n=0,nr=0,a,b,i; printf(“Enter the coordinates:\n”); while(scanf ( “%d %d “, &a, &b) == 2) { x[n]=a; y[n]=b; n++; } if (!feof(stdin)) { printf(“Wrong\n”); } else { for(i=0;i<n;i++) printf("%d %d\n", x[i], y[i]); } return 0; }

scan中的scanset行为

我试图在scanf中使用scanset做一些事情但是卡在某处。 我写的时候 char s1[250]; scanf(“%[AZ]s”,s1); input : AHJHkiuy Output: AHJH 有了这个, scanf(“%[^\n]s”,s1); input: abcd ABCD hie output: abcd ABCD hie /*that is reading white space also (till \n) */ 现在我的问题是,如果我提供输入: ABCDahaj ahajABCD ajak12347ab 并希望输出为: ABCDahaj ahajABCD ajak 那么格式字符串应该如何写? 也就是说,如何使用这个扫描设备?

scanf何时开始和停止扫描?

当按下Enter键时, scanf似乎开始扫描输入,我想用下面的代码validation这一点(为简单起见,我省略了错误检查和处理)。 #include int main(int argc, char **argv) { /* disable buffering */ setvbuf(stdin, NULL, _IONBF, 0); int number; scanf(“%d”, &number); printf(“number: %d\n”, number); return 0; } 这是另一个问题,在我禁用输入缓冲之后(只是为了validation结果;我知道我应该接下来 – 实际上从不这样做,以防它干扰结果),输出是(注意额外的提示): $ ./ionbf 12(space)(enter) number: 12 $ $ 这与输入缓冲启用时的输出不同(无额外提示): $ ./iofbf 12(space)(enter) number: 12 $ 启用缓冲区时似乎消耗了新行字符。 我在两台不同的机器上进行了测试,一台安装了gcc 4.1.2和bash 3.2.25,另一台安装了gcc 4.4.4和bash 4.1.5,结果两者相同。 问题是: 如何在启用和禁用输入缓冲时解释不同的行为? 回到原来的问题, scanf什么时候开始扫描用户输入? 角色进入的那一刻? […]

scanf()如何在操作系统内部工作?

我一直在想如何scanf()/ printf()实际上在硬件和操作系统级别上工作。 数据在哪里流动以及操作系统在这些时间到底做了什么? 操作系统会调用什么? 等等…

Char * p和scanf

我一直试图找出以下代码失败的原因,但我找不到。 请原谅我的无知,让我知道这里发生了什么。 #include int main(void){ char* p=”Hi, this is not going to work”; scanf(“%s”,p); return 0; } 据我所知,我创建了一个指针p到内存大小为29 + 1的连续区域(对于\ 0)。 为什么我不能使用scanf来改变它的内容? PS请纠正我如果我对char *说错了。

为什么scanf扫描空值

在下面的代码中: #include int main(){ char *name; int age; char *gen; printf(“Your name:”); scanf(“%s”,name); printf(“Your age:”); scanf(“%d”,&age); printf(“Your gender:”); scanf(“%s”,gen); printf(“*****************\n”); printf(“%s is a %d years old %s \n”,name,age,gen); return 0; } 当我像这样运行它: Your name:tom Your age:20 Your gender:male ***************** tom is a 20 years old (null) 正如你所看到的,gen是一个空值,为什么scanf无法读入gen但前两个读数成功?

C ++格式字符串的C ++等价物

我有一个从键盘读取的C程序,如下所示: scanf(“%*[ \t\n]\”%[^A-Za-z]%[^\”]\””, ps1, ps2); 为了更好地理解该指令的作用,我们将格式字符串拆分如下: %*[ \t\n]\” =>读取所有空格,制表符和换行符( [ \t\n] ),但不将它们存储在任何变量中(因此为’ * ‘),并将继续读取,直到遇到双精度引用( \” ),但双引号不输入。 一旦scanf()找到双引号,读取所有不是字母的字符到ps1。 这是通过……完成的 %[^A-Za-z] =>输入任何不是大写字母’A’到’Z’和小写字母’a’到’z’的东西。 %[^\”]\” =>读取所有剩余的字符,但不包括ps2的双引号( [^\”] ),字符串必须以双引号( \” )结束,但双引号没有输入。 有人可以告诉我如何在C ++中做同样的事情 谢谢

结构混乱

我想创建一个函数,它获取一本书的标题和作者,如果它可用或不可用则返回0或1,通过将它们与给定的结构数组进行比较…..编译器显示: structs.c:10:28: error: expected ‘)’ before ‘title’ structs.c: In function ‘main’: structs.c:59:21: error: expected expression before ‘bookRecord’ structs.c:60:13: error: expected expression before ‘bookRecord’ structs.c:61:9: warning: implicit declaration of function ‘requestBook’ structs.c:61:23: error: expected expression before ‘bookRecord’ 这是代码: #include #include #include #define TRUE 1 #define FALSE 0 #define NUM_BOOKS 5 int requestBook(bookRecord title[],bookRecord author[]){ /* compiler […]

scanf正在收集错误的输入

#include int main(void) { double c; scanf(“%f”, &c); printf(“%f”, c); } 这是我试图编写的程序的一个漏洞,但是我对这个简单的问题也有同样的看法。 当我运行它并输入“1.0”时,它打印出“0.007812”。 我看过几个与我类似的问题,但找不到合适的答案。