【问题标题】:C fscanf reading data from file errorC fscanf从文件读取数据错误
【发布时间】:2017-04-02 14:01:24
【问题描述】:

我是 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

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

感谢您的宝贵时间!

【问题讨论】:

  • 不幸的是仍然崩溃
  • 试过了,还是崩溃:(
  • 你摇滚!!!!!!!!!

标签: c crash scanf


【解决方案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");
    }
}

&amp; 放在您希望读取的每个变量之前。

喜欢:

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。我认为第一个选择会很好。

【讨论】:

  • 现在开始运行了,但是没有收到所有信息,我会更新原帖
  • 编写一个将输入 ',' 更改为 '.' 的代码应该不难
【解决方案2】:

我就是这样做的,但我自己当然不是 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的末尾,否则,它会添加一个空格, 或分隔符。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多