Tag: scanf

scanf:内置宏(#define常量)的模板

我有一些像这里的代码: #define MAXSIZE 100 int main() { char str[MAXSIZE+1]; scanf(“%100s”, str); … 问题是我仍然有“幻数”100,虽然定义了MAXSIZE。 有没有办法正确地“插入”MAXSIZE到scanf格式字符串? (纯C,c-99标准)

在使用GDB的Eclipse CDT中,Scanf似乎无法在调试模式下工作

在调试模式下运行此代码时: #include #include int main() { int a, b, c; scanf(“%d%d%d”, &a, &b, &c); printf(“Values entered: %d %d %d\n”, a, b, c); return EXIT_SUCCESS; } 该程序不会请求任何用户输入,只会输出: 输入值:18 78 2130026496

fscanf无法正确读取浮点数

—这是一个家庭作业问题— 我在使用fscanf从文本文件中读取浮点值时遇到问题。 基本上我正在尝试从文件中读取浮点值并将它们存储在动态数组中。 输入文件每行有两个浮点数。 所以一行可能是“0.85 7.34”(不带引号)。 所以我试图使用fscanf(fp,“%f%f”,&coordinates [i],&coordinates [i ++])来读取2个浮点值。 当我打印它显示为0.00000。 下面是我编写的代码及其生成的输出。 #include #include int main (int argc, char *argv []) { FILE * fp = fopen(“nums”, “r”); float *coordinates; float *tmp; int i = 0; int ARRAY_SIZE = 5; coordinates = malloc(5*sizeof(float)); while (fscanf(fp,”%f %f”, &coordinates[i], &coordinates[i++]) > 1) { printf(“iteration# %d | coord1 […]

对于循环扫描,c中的时间少一个

用户输入一串字符,但在此之前输入字符串的大小。 然后我必须阅读并计算每个字母输入的次数。 这是我的代码: #include char ab[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’}; //Size = 26 int main(void) { int i, j, size, counter[26]; char temp; for(i=0; i<26; i++) { counter[i]=0; } scanf("%d",&size); for(i=0; i<size; […]

Scanfvalidation

有人可以帮我validation我在下面的Scanf的输入。 如果Scanf在不正确的范围内或者不是整数,我希望程序要求重新输入数据。 我在使用if语句之前放入了do while循环,但是当我编译它时,第一个printf和scanf只是循环 #include #include int MenuLoop = 0; int MaxPackets = 4; int currentPackets= 0; int menu; /********************************************************* * Node to represent a Cat which includes a link reference* * a link list of nodes with a pointer to a Cat Struct * * would be better but this is for illustartion only! […]

初学者C – 以相反的顺序打印用户的浮动输入

我正在编写一个程序,它使用1个语句来读取用户的6个浮点数。 然后让它打印3行中的6个数字,满足以下所有要求: (1)数字按照读入的相反顺序打印 (2)它们在3行上:第一行有1个数字,下一行有2个数字,最后一行有3个数字 (3)将数字对齐,使它们采用列格式,右对齐,小数点后1位 这是我对前两个要求的尝试 #include int main(void) { //variable definitions float f1,f2,f3,f4,f5,f6; printf (“Enter 6 float numbers, separated by commas: “); scanf (“%f1,%f2,%f3,%f4,%f5,%f6”,&f1,&f2,&f3,&f4,&f5,&f6); printf (“%f6\n”,f6); printf (“%f5,%f4\n”,f5,f4); printf (“%f3,%f2,%f1\n”,f3,f2,f1); return 0; } 对于我的初学者来说,它非常有意义。 这是我运行时的结果 输入6个浮点数,用逗号分隔:0.2,3.2,0.1,0.5,0.6,0.7 数字是: -107374176.0000006 -107374176.0000005,-107374176.0000004 -107374176.0000003,-107374176.0000002,0.2000001 按任意键继续 。 。 。 所有这些都是垃圾输出,除了最后一个。 感谢所有的帮助!

使用feof()使用fscanf()

这是我的代码。 #include void main(){ FILE *fp; int a,b; fp=fopen(“hello.txt”,”r”); while(!feof(fp)){ fscanf(fp,”%d %d”,&a,&b); printf(“%d %d\n”,a,b); } } 我的hello.txt是 1 2 3 4 我的输出是 1 2 3 4 4 4 为什么我的最后一行被打印两次。 还没有fp达到EOF? 此外,stackoverflow中的标记说Usually, when it is used, the code using it is wrong. 这是什么意思? 谢谢。

扫描包含C中空格的字符串

在我的代码中: scanf(“%s”, &text); printf(“%s\n”, text); 输入: hi how are you 输出: hi 并不是 hi how are you 我该怎么办才能修好它?

从文本文件中读取字符串和整数

假设我有一个文件看起来像这样 51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers 77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers 31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping 52.03 12.02 12.04 22.00 31.98 61.97 Big Bank 44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping 93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs 我想用fscanf读取这个文件字,将数字放在浮点数组中,将字放在字符串数组中。 但是,经过几个小时的艰苦思考,我仍然无法弄清楚如何解决这个问题。 void […]

C scanf()问题

我是C的新手,不能为我的生活解决我在这里做错了什么。 第一个scanf工作正常,变量在读入时打印出来。第二个scanf似乎没有正确读取输入。 输入格式为“char int int”,即b 4 4 当我打印opb x和y out时,opb =“”,x = 13238272,y = 0。 任何想法?…..注意我已经删除了问题下面的代码 int main(void) { /*initialize variables*/ int width, height; char op; /*grid input*/ scanf(“%c %d %d”, &op, &width, &height); /*check conditions*/ if (op != ‘g’ || width>100 || height>100 || width*height<10 || width<1 || height<1) { printf("grid-error\n"); return 0; } […]