c - C编程:读取文件并存储在struct数组中

标签 c struct structure scanf

我正在尝试通过fscanf读取文件test.txt并将其存储在struct数组中。这就是我尝试过的。这里的问题是fscanf无法正常运行。读取文件后,我也尝试在屏幕上打印该文件,但是它不起作用。

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

struct Item {
   double value;
   int unit;
   char name[50];
};

int load(struct Item* item, FILE* data);
void display(struct Item item, int variableA);

int main()
{
  struct Item I;
  int i;
  char ck;
  ck =  fopen("test.txt", "r");
  if (ck)
  {
    for (i = 0; i < 3; i++)
    {
        load(&I, ck);
        display(I, 0); //DISPLAY FUNCTION THAT READS test.txt and DISPLAYS
    }
    fclose(ck);
  }
return 0;
}


int load(struct Item* item, FILE* data)
{
    fscanf(data, "%d,%.2lf,%s\n", &(*item).unit,&(*item).value,&(*item).name);
    return 0;
}

void display(struct Item item, int variableA)
{
printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
return;
}


这是我在test.txt文件中所拥有的:

205,11.20,John Snow
336,23.40,Winter is coming
220,34.20,You know nothing


错误:程序在编译时带有一些警告,但是执行代码时出现分段错误。

知道为什么吗?

输出预期:应从test.txt文件读取OUTPUT,并应将其显示在屏幕上。

最佳答案

程序中的多个问题:

1。

char ck;
ck =  fopen("test.txt", "r");


fopen返回FILE*,而不是char,使用

FILE* ck = fopen(...);


2。

fscanf(data, "%d,%.2lf,%s\n", &(*item).unit,&(*item).value,&(*item).name);


始终检查fscanf的返回值,如果它小于您请求的字段数,则以下对fscanf的调用不太可能达到您的期望。另外,*item.unititem->unit相同,请使用item->unit,因为它更短且更干净:

int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
if (ret != 3) { // error }


第三,%s与一系列非空格字符匹配,因此,当fscanf读取“ John”时,它将停止,并且下一个fscanf调用将在读取“ Snow”的同时期望一个整数。

因此,要输入带空格的字符串,请改用fgets,并记住最后要删除换行符。

请尝试以下操作:

int main(void)
{
    struct Item I;
    int i;
    FILE* ck;
    int ret;
    ck =  fopen("test.txt", "r");
    if (ck)
    {
            for (i = 0; i < 3; i++)
            {
                    ret = load(&I, ck);
                    if (ret < 0)
                            break;
                    display(I, 0); //DISPLAY FUNCTION THAT READS test.txt and DISPLAYS
            }
            fclose(ck);
    }
    return 0;
}

int load(struct Item* item, FILE* data)
{
    int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
    if (ret != 2) {
            return -1;
    }
    fgets(item->name, sizeof item->name, data);
    item->name[strlen(item->name)-1] = '\0';
    return 0;
}

void display(struct Item item, int variableA)
{
    printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
    return;
}


它输出:

$ ./a.out
|205 |       11.20|            John Snow |***
|336 |       23.40|     Winter is coming |***
|220 |       34.20|     You know nothing |***

关于c - C编程:读取文件并存储在struct数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835625/

相关文章:

c - 从文件读取输入的矩阵运算

c - 出现错误 : "variable...is uninitialized when used within its own initialization" in C

c - 使用 GNU 程序集访问指向结构的 *next 指针

c - 结构实现建议

c - 在所有线程上读取相同的结构

c# - 为什么 `j=++i+++i;` 在 C# 和 C 中的输出不同?

在 C 中,您能否将具有可变数量参数的函数作为参数传递给另一个函数?

arrays - 如何在 HiveQL 中将字符串转换为结构数组

python - 扁平化Python中的嵌套结构

c - 结构指针实现方法