Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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
如何从fscanf循环中存在?_C - Fatal编程技术网

如何从fscanf循环中存在?

如何从fscanf循环中存在?,c,C,当没有更多数据可读取时,我希望从循环中存在。 我试过了!feoffp,EOF,但它无限循环。 如果我改为fscanffp,str,&str==1,它将无法读取任何内容。 代码如下: void readFile(){ FILE *fp; int i=0, j=1; int* arr; char str[45]; fp = fopen("c:\\Defuzzification.txt","r"); // read mode if (

当没有更多数据可读取时,我希望从循环中存在。 我试过了!feoffp,EOF,但它无限循环。 如果我改为fscanffp,str,&str==1,它将无法读取任何内容。 代码如下:

void readFile(){

    FILE *fp;   
    int i=0, j=1;   
    int* arr;
    char str[45];


    fp = fopen("c:\\Defuzzification.txt","r"); // read mode

    if (fp == NULL) {       
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }



    arr = (int*)calloc(SIZE, sizeof(int));

    while( fscanf(fp, "The different between frame %d and %d :%d",  &i, &j, arr)  == 1 ){
        fscanf(fp, "The different between frame %d and %d :%d",  &i, &j, arr);
        printf("Display The different between frame %d and %d :%d\n",  i, j, *arr);
        i++;
        j++;    
    }

    fclose(fp); 
}
扫描FFP时,帧%d和%d之间的差异:%d、&i、&j、arr!=EOF=>>循环无限

fscanffp时,帧%d和%d之间的差异为:%d,&i,&j,arr==1=>>不读取任何内容

输入数据:

The different between frame 0 and 1 :80
The different between frame 1 and 2 :58.18
The different between frame 2 and 3 :77.59
The different between frame 3 and 4 :71.24
The different between frame 4 and 5 :68.46
The different between frame 5 and 6 :75.81
The different between frame 6 and 7 :41.35

在下面的语句中,如果所有输入操作都成功,fscanf将返回3

while( fscanf(fp, "The different between frame %d and %d :%d",  &i, &j, arr)  == 1 ){
将其更改为:

while( fscanf(fp, "The different between frame %d and %d :%d",  &i, &j, arr)  == 3 ){
还有一件事。。。您有数字80、58.18等。这些是浮点数而不是整数。您不应该使用浮点格式并将其读入浮点变量吗

float number;
while( fscanf(fp, "The different between frame %d and %d :%f",  &i, &j, &number)  == 3 ){
更新

您需要进行以下更改:

在fscaf的格式字符串中,将空格作为第一个字符。这将跳过零个或多个空格,包括换行符

使用浮点数读取最后一个数据。否则,数字的小数部分将留在输入流中,随后的读取操作将失败

您需要将while语句的条件中fscanf的返回值与3进行比较。如果fscanf能够成功读取所有三段数据,它将返回3

使用以下代码块读取数据对我很有用:

float number;
while( fscanf(fp, " The different between frame %d and %d :%f",  &i, &j, &number)  == 3 ){
    fscanf(fp, " The different between frame %d and %d :%f",  &i, &j, &number);
    printf("Display The different between frame %d and %d :%f\n",  i, j, number);
    i++;
    j++;    
}

这不是一个好方法,但我用它来做whilefscanffp,%s,str=EOFusing fscanffp、str和str将导致未定义的行为,您正在用刚才输入的内容覆盖字符串指针read@sasha仅适用于%s;如果OP正在扫描其他内容,则与正数的匹配为better@user2301281你的帖子很不清楚;请发布到目前为止您尝试过的实际代码,并确保它在编译之前已编译posting@MattMcNabb哦,好的,谢谢你告诉我改为3,但它只读取第一个数据。顺便说一句,为什么要改为3?当我把它改为float时,读取的值变为零。编辑:float*arr,arr=float*callocSIZE,sizeoffloat;我不理解更新1。当扫描FFP时,帧%d和%d之间的差异是否会变成=>>d、&i、&j,arr==3??但输出只是读取两行数据,然后停止。@user2301281,不确定您做了什么。请参阅的工作代码。它不会跳过第一行数据。它只是每隔一行打印一次,因为您有两条fscanf语句,但只有一条printf语句。