Tag: scanf

使用fprintf和fscanf时出错

我有一个存档results.csv ,我需要读取此存档的第一行并在output.txt上打印出来。 不知怎的,它在所有东西之后打印随机字符,我无法弄清楚出了什么问题。 命令: ac results.csv 第一行: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral output.txt: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral,(!£,(!£,(!£,(!£,(!£,@,£,(!£,(!£ #include #include #include #include typedef struct { char *line1; char *line1a; char *line1b; char *team1; char *team2; char *reason; char *city; char *country; char *neutral_field; }data; void open_input(char *argv[], FILE **input) { if((*input=fopen(argv[1], “r”)) == NULL) { printf(“%s not found\n”, argv[1]); exit(1); } } void […]

如何在c中没有`scanf()`的情况下得到整数和浮点输入?

如何在不使用scanf()情况下将用户给定的整数和浮点值赋给变量或数组? 就像我们有char和string getchar , fgetc , fgets …等,是否有浮点数和整数的函数?

在c中正确声明变量

对于c编码器来说,下面的内容肯定会非常简单,但我正在编写一个小程序来模拟一些名为gomoku的游戏。 对于用户,您必须输入一个整数N,其对应于’N次N’平方,其由’N次N’整数组成。 所以代码运行得很好,但我有一个简单的问题:当我输入’N次N’整数时,我做了一些 int N; scanf(“%d”,&N); char c[N][N]; while (i<N){ scanf("%s\n",&c[i]); i++; } 然后我将每个c[i]的char转换为int进行一些涉及c[i][j] ,这非常不自然。 但是如果我必须声明int c[N][N] ,就不可能像在while循环运行时那样输入相同的整数c[i][j] 。 有没有人有想法声明int c[N][N] ,输入整数,然后用整数c[i][j]计算时计算相同的? 最好的,纽本

C:sscanf问题

我有这样的文本文件: 2 A 10 5 B 31 2 C 6 6 我想读一个变量中的第一个行号 并在3个变量中读取每行的空格分隔的3个值列表。 我写了这段代码: iF=fopen(fileName,”r”); fgets(tmp,255,iF); sscanf(tmp,”%d”,&interval); while(!feof(iF)){ cur=(P *)malloc(sizeof(P)); fgets(tmp,255,iF); sscanf(tmp,”%c %d %d”,&Name,&AT,&ET); cur->jobName=Name; cur->arrivalTime=AT; cur->execTime=ET; add_to_list(head,cur); } 它适用于1,3,4线,但不适用于2号线! 在第2行它什么都没有存储! 当我在调试器中检查时,文件中有一些奇怪的字符(\ 342 \ 200 \ 252),我不知道它们来自哪里!? 有什么问题? 谢谢

使用scanf检查输入参数

尝试使用scanf检查我有适当数量的输入(在这种情况下为2),并且代码工作正常,但如果我输入1输入它只是等待一秒钟,如果我放入3它只是丢弃第三,我做的任何事都不会返回错误信息。 已经找到了答案,但找不到我可以使用的任何东西,我问的原因是我用于这个主题的教科书的代码与我在这里完全相同(我复制了一个不同的例子为int_swap字为一句话,它似乎也没有用?任何想法?对不起,如果这是一个愚蠢或简单的问题。 #include #include void int_sort2(int*, int*); int main(int argc, char* argv[]) { int x, y; printf(“Please enter 2 numbers : “); if(scanf(“%d%d”, &x, &y) != 2) { printf(“Error in numbers entered\n”); exit(EXIT_FAILURE); } printf(“The original order was %d, %d\n”, x, y); int_sort2(&x, &y); printf(“The sorted order is : %d, %d\n”, x,y); return 0; } […]

如何从文件中将数字扫描到数组?

假设我有一个带有数字的文件:1 2 3 4 5 6 7 8 9 10 -1 我想读取该文件并存储该文件的所有值,停在控制变量-1。 因此,如果我将该数组打印到另一个文件,它看起来像:1 2 3 4 5 6 7 8 9 10 这给了我所有的数字,但它包括-1。 怎么能掉掉-1? int arr[100]; int n; while (scanf(“%d”,&arr[n]) > 0) n++;

为什么scanf要求输入两次,但仅在第一次循环迭代中?

首先,对于这个问题,这似乎是重复的,但在我的情况下,为什么这只发生在第一次循环迭代中(第一个数组元素的输入)。 为什么不呢? 我的代码: #include “stdio.h” int main(int argc, char const *argv[]) { int a[5]; int i; for (i = 0; i < 5; i++) { printf("Input a[%d]:\n", i); int x = scanf("%d ", &a[i]); // notice the white-space after %d } for (i = 0; i < 5; ++i) { printf("a[%d]=%d\n", i, a[i]); } } […]

使用%d扫描字符

我在下面有一个具体的例子,如果输入整数(参见output1),它可以正常工作,当我尝试使用scanf函数调用中的%d说明符扫描一个字符时,我得到下面的输出2。 所以,我的问题是如果输入一个char我希望类型说明符应该将它转换为等效的int值,如果不是垃圾值,即使在任何一种情况下它应该print / segfault。 但是,在这里我得到了连续的打印,我觉得这是错误的,因为scanf每次都被绕过。 我很不确定在后台发生了什么,并且想知道同样的事情。 #include int main() { int a; while (1){ printf(“enter a number:”); scanf(“%d”, &a); printf(“entered number is %d\n”, a); } return 0; } 输出1 : > enter a number:1 > entered number is 1 > enter a number: > 3 > entered number is 3 > enter a number:4 > […]

循环中的C scanf在没有输入的情况下自动继续

我正在尝试在数组中输入,我希望输入如下。 5 (Number of the second dimensions in the array) 2 (Number of the first dimensions in the array) 所以我们在这个例子中得到了一个数组deeln [2] [5]。 我尝试使用以下代码获取它: #include #include #include bool isinarray(int val, int *arr, int size){ int countimp; for (countimp=0; countimp < size; countimp++) { if (arr[countimp] == val) return true; } return false; } int main(void){ int […]

scanf是不是在循环中等待输入?

我是Objective-C的新手,这是我的第一个交互式程序。 我已经学习了大约两个星期了。 所以,我的问题是:通常我注意到当你连续多个scanf时,他们每个都在等待输入 – 但是在这种情况下,我要求帐户所有者名称和余额 – 它会触发两个NSLog函数而不是等待第一次输入。 这是我的主要内容: int main(int argc, char* argV[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; bank *columbiaBank = [[bank alloc] init]; int iteration = 0; while (true) { int selection = 0; NSLog(@”\n1. Add Account \n2. Remove Account \n3. Modify Account \nWhat would you like to do?:”); scanf(“%i”, &selection); […]