Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文件中读取多个实例_C_File_Numbers - Fatal编程技术网

C 从文件中读取多个实例

C 从文件中读取多个实例,c,file,numbers,C,File,Numbers,所以我从一个文件中读取数据,格式中有数字。例如数字1 2。所以我从这个文件向用户的输出应该是1x3 2x2 #include<stdio.h> int main(){ int number; int times; FILE* ifp = fopen("counting.txt", "r"); if (ifp == NULL) { printf("Error opening counting.txt\n"); re

所以我从一个文件中读取数据,格式中有数字。例如数字1 2。所以我从这个文件向用户的输出应该是1x3 2x2

#include<stdio.h>




int main(){

int number;
int times;



FILE* ifp = fopen("counting.txt", "r");
    if (ifp == NULL) {
            printf("Error opening counting.txt\n");
            return 1;

    }

while (fscanf(ifp, "%d %d", &number, &times)== 2){
    printf( "%dx%d", number, times);


}

return 0;
}
#包括
int main(){
整数;
整数倍;
文件*ifp=fopen(“counting.txt”、“r”);
如果(ifp==NULL){
printf(“错误打开counting.txt\n”);
返回1;
}
而(fscanf(ifp,“%d%d”、&数量和次数)==2){
printf(“%dx%d”,数字,次数);
}
返回0;
}
我遇到了一个问题,上面的代码可以编译,但在(fscanf(ifp,%dx%d,&number,×)==2)时没有返回任何输出{
while (fscanf(ifp, "%dx%d", &number, &times)==2){
    int looper = 0;
    while(looper < times){
        printf("%d%s", number, " ");
        looper++;
    }
}
int-looper=0; while(活套<次){ printf(“%d%s”,数字“”); looper++; } }
您的代码没有为我编译

事实上,您的代码不会为任何人编译,因为这行代码:

while (fscanf(ifp, "%dx%d" number, times)==2){
while (fscanf(ifp, "%dx%d", &number, &times)==2){
编号前应加逗号。我已编辑了您的答案以解决此问题。但是,也会出现其他编译错误

当代码未编译时询问有关代码的问题是不好的,除非您想知道它为什么未编译

一旦我修复了代码中的所有其他bug,我会得到以下结果:

#include <stdio.h>

int main(){
    int number;
    int times;

    FILE *ifp = fopen("counting.txt", "r");

    while (fscanf(ifp, "%dx%d", number, times)==2){
        printf("%d",number);
    }

    return 0;
}
因此:

bob.c: In function ‘main’:
bob.c:9:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
     while (fscanf(ifp, "%dx%d", number, times)==2){
     ^
bob.c:9:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
bob.c:10:17: error: expected expression before ‘%’ token
         printf( %d);
这就很明显发生了什么

我们使用以下行修复此问题:

while (fscanf(ifp, "%dx%d" number, times)==2){
while (fscanf(ifp, "%dx%d", &number, &times)==2){

你必须想办法解决问题。

你应该在
fscanf
中使用
&number
×
。另外,
printf(%d)
没有编译。对,是的,我不知道如何以我需要的格式输出,printf只是未完成的代码。那么,为什么不循环
次,并在循环中打印
?做某事的次数:
for(inti=0;i