C fscanf从文件错误中读取数据

时间:2017-04-02 14:01:25

标签: c crash scanf

我是C的新手,我正在尝试编写一个程序,它将从文件中读取数据并将它们存储在一个数组中(所以我以后可以使用它们)。

#include <stdio.h>
#include <stdlib.h>


int day[3], month[3], year[3], hour[3], minute[3], second[3];
float value[3];


int main()
{  
    // Open the file for read
    FILE* file1 = fopen("test.txt", "r");

    // Safety check
    if (file1 == NULL)
    {
        printf("Error: file1 == NULL\n");
        getchar();
        return 0;
    }

    // Open the new file for write
    FILE* file2 = fopen("new.txt", "w");

    // Safety check
    if (file2 == NULL)
    {
        printf("Error: file2 == NULL\n");
        getchar();
        return 0;
    }


    int j = 0;

        for (j = 0; j < 4; j++)
        {
            int count = fscanf(file1, "%d-%d-%d %d:%d:%d %f", &day[j], &month[j], &year[j], &hour[j], &minute[j], &second[j], &value[j]);
            printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
            // Check for finising early
            if (count == EOF)
            {
                printf("EOF");
            }
        }

    // Close the original file
    fclose(file1);
    // Close the target file
    fclose(file2);  

    getchar();
}

该文件包含以下格式的数据:

20-03-17 08:49:01 28,515
20-03-17 08:49:31 29,1837
20-03-17 08:50:01 27,845

评论后编辑:

现在代码不再崩溃,它几乎完美无缺!

这就是出现的结果:

20-3-17 8:49:1 28.514999
20-3-17 8:49:31 29.183701
20-3-17 8:50:1 27.844999
1105469112-1-20 17:8:49 0.000000

有没有办法解决小数问题? 什么是最后一行?

感谢您的时间!

2 个答案:

答案 0 :(得分:1)

在这里:

for (j = 0; j < 3; j++)
{
    int count = fscanf(file1, "%i-%i-%i %i:%i:%i %f ", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    // Check for finising early
    if (count == EOF)
    {
        printf("EOF");
    }
}

&放在您想要阅读的每个变量之前。

像:

for (j = 0; j < 3; j++)
{
    int count = fscanf(file1, "%i-%i-%i %i:%i:%i %f ", &day[j], &month[j], &year[j], &hour[j], &minute[j], &second[j], &value[j]);
    printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    // Check for finising early
    if (count == EOF)
    {
        printf("EOF");
    }
}

问题编辑后:

好的,问题是您的数据文件(test.txt)格式化为

20-03-17 08:49:01 28,515

,不是

20-03-17 08:49:01 28.515

因此%f只能读取28,而不是28.515(因为,不用于浮点数)。您有两种选择:(1)在数据文件中将28,515更改为28.515(2)单独阅读28515。我认为第一选择会很好。

答案 1 :(得分:0)

我就是这样做的,但我自己肯定不是C语言编程的大师。 我认为最好是使用stat,但文件大小等。

您可以执行以下操作:

   #include <stdio.h>
   #include <string.h>
   #include <stdlib.h>
   #include <sys/stat.h>
   #define dir "/path/to/file"
   #define MAX_WORDS 32
   int main() {
       FILE *fp;
       struct stat stz;
       stat(dir, &stz);
       int charz;
       char data[4096], buff[8];
       char *words[MAX_WORDS];         
       if (stz.st_size > 0) {
            fp = fopen(dir, "r");
            fseek(fp, 0, SEEK_SET);
            for (i = 0; i < stz.st_size; i++) {
                charz = fgetc(fp);
                if (charz == 10) {
                    strcat(data, " ");

                } else {
                    sprintf(buff, "%c", charz);
                    strcat(data, buff);
                }
            }
            fclose(fp);
            words[0] = strtok(data, " ");
            for (i = 0; i < MAX_WORDS; i++) {
                if (!words[i]) {
                    break;
                }
                words[i+1] = strtok(NULL, " ");
            }
       } else {
             printf("No data available in given directory");
       }
       return 0;
   }

这将每个字节放入变量charz,然后放入单个字符缓冲区,如果字符不等于10(NewLine),则将所述字符连接到字符串data的末尾,否则,它增加了一个空格或分隔符。

然后,您可以通过拆分字符串来初始化单词数组。

相关问题