以特殊方式从文件中读取浮点数

时间:2017-03-11 15:52:59

标签: c file-io floating-point scanf

我正在尝试从2D数组中读取文件中的数字,我必须跳过第一行和第一列,其余都必须保存在数组中,我尝试过使用sscanf,fscanf甚至strtok ()但悲惨地失败了。所以请帮我解决这个问题。 Thanx提前,

Link to the file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]){
FILE *f=fopen("Monthly_Rainfall_Himachal.txt","r");
float data[12][12];
int i,j;
char newLine[1000];
fgets(newLine,1000,f);
char* item,waste;
i=0;
while(1)//read file line by line
{
    fscanf(f, "%s %f %f %f %f %f %f %f %f %f %f %f %f ", waste, &data[i][0], &data[i][1], &data[i][2], &data[i][3], &data[i][4], &data[i][5], &data[i][6], &data[i][7], &data[i][8], &data[i][9], &data[i][10], &data[i][11]);
    i++;
    if(feof(f))break;
}
fclose(f);

for(i=0 ;i<12 ;i++){
    for(j=0 ;j<12 ;j++){
        printf("%.1f\t",data[i][j]);
    }
    printf("\n");
}
return 0;
}

1 个答案:

答案 0 :(得分:3)

问题:

  1. 您不会检查fopen是否成功打开文件,并盲目地认为是这样。

    检查其返回值:

    if(f == NULL)
    {
        fputs("fopen failed! Exiting...\n", stderr);
        return EXIT_FAILURE;
    }
    
  2. 您可以使用scanf阅读并放弃,而不是阅读并存储第一行:

    scanf("%*[^\r\n]"); /* Discard everything until a \r or \n */
    scanf("%*c");       /* Discard the \r or \n as well */
    
    /* You might wanna use the following instead of `scanf("%*c")` 
       if there would be more than one \r or \n 
    
    int c;
    while((c = getchar()) != '\n' && c != '\r' && c != EOF);
    
       But note that the next fscanf first uses a `%s` which
       discards leading whitespace characters already. So, the
       `scanf("%*c");` or the while `getchar` loop is optional 
    */
    
  3. 您有一个未使用的字符指针item和一个字符变量waste。这些都是不必要的。所以,删除它们。
  4. 在非常长的fscanf行中,您首先尝试将字符串扫描到一个字符变量中,该变量调用Undefined Behavior并且事情变得混乱。您还需要检查其返回值以查看它是否成功。

    fscanf行替换为以下内容:

    if(fscanf(f, "%*s") == EOF)
    {
        fputs("End Of File! Exiting...\n", stderr);
        return EXIT_SUCCESS;
    }
    for(j = 0; j < 12; j++)
    {
        if(fscanf(f, "%f", &data[i][j]) != 1)
        {
            fputs("End Of File or bad input! Exiting...\n", stderr);
            return EXIT_SUCCESS;
        }
    }
    
  5. 您假设输入最多为12行,但如果它包含超过12行,则由于数组溢出,您的代码将调用未定义的行为。

    检查i的值以及feof以确保其不超过11​​:

    if(i >= 12 || feof(f))
    
  6. 注意:我没有测试上面的任何代码。如果我犯了错误,请纠正我。谢谢!