将文件中的数据存储到struct数组中

时间:2015-02-16 20:42:11

标签: c arrays file struct storage

我正在尝试创建一个程序,在该程序中读取包含未知行的文件,然后按空格分割数据,然后将其添加到结构中。我在读取数据并将其添加到结构中时遇到问题。尝试使用GCC编译时出现一些错误。他们来了。

program1.c: In function ‘initialize’:
program1.c:54:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat=]
   if(fscanf(fp, "%s %f %d %s\n", *data[counter].type, data[counter].literAmount, *data[counter].miles, *data[counter].color) !=4) {
   ^
program1.c:57:2: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘float *’ [-Wformat=]
  printf("data stored is: %s %f %d %s\n", *data[counter].type, data[counter].literAmount, data[counter].miles, *data[counter].color);
  ^
program1.c:57:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int *’ [-Wformat=]

以下是该计划的代码。

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

typedef struct importedData {
    char *type[20];
    float *literAmount;
    int *miles;
    char *color[20];
}DATA;



int main() {
    int userInput;
    initialize();
    //Do while loop to loop through  menu until exit.
    do {
    //Print statements to put out the output options
        printf("Choose the option you would like to preform \n");
    printf("1.Sort data from the float value & print high to low \n2.Sort data by the float value & print low to high\n3.Sort data by the int value & print high to low\n4.Sort data by the int value & print low to high");
    scanf("%d", &userInput); //Scanner to take in the keyboard input


    } while (userInput != 5);


    return 0;
}

int numberOfLines() {
    FILE *fp = fopen("hw3.data", "r"); //Opens file name and in the read format
    int c = 0; //Count the number of characters
    int count = 0; //Number of lines

    if(fp == NULL) { //If file was not able to be opened program will exit
        printf("Unable to read file. Closing");
        return 0;
    }
    while((c = fgetc(fp)) != EOF) { //Get the character and make sure it isnt the END OF FILE
        if(c == '\n') { //If the charcter is set to \n (New Line) will add one to counter
            count++;
        }
    }
    fclose(fp);
    return count;
}

int initialize() {
    DATA *data = malloc(numberOfLines() * sizeof(*data));
    FILE *fp = fopen("hw3.data", "r"); //Opens file name and in the read format
    int counter = 0;
    int totalIteragions = numberOfLines();
    while(counter < totalIteragions) {
        if(fscanf(fp, "%s %f %d %s\n", *data[counter].type, data[counter].literAmount, *data[counter].miles, *data[counter].color) !=4) {
        return -1;
    } 
    printf("data stored is: %s %f %d %s\n", *data[counter].type, data[counter].literAmount, data[counter].miles, *data[counter].color);
    }
}

有人可以向我解释为什么会出现这个错误以及如何修复它以及将来捕获这样的错误吗?谢谢你的帮助。

编辑:我不必在结构指针中创建变量吗?我假设因为我使用malloc创建了一个结构数组,所以已经分配了内存位置,所以当你输入数据时它会将它存储到每个内存位置。正确的吗?

1 个答案:

答案 0 :(得分:1)

打印和扫描不同。

使用scanf,您正在将整数读入内存中的某个位置,因此匹配%d的参数应为int*,而不是int

使用printf,您将打印一个整数,因此匹配%d的参数应为int,而不是int*。与%f相同的交易 - 在尝试打印时,您应该传递floatdouble,而不是float *

您的代码还有许多其他问题,我倾向于同意Matt McNab - 您似乎随意抛出*。我建议在继续之前寻找关于指针的介绍性指南。然后阅读realloc(无需重复文件两次......)

示例结构,减少100%* s:

typedef struct importedData {
    char type[20]; // better be careful reading into this
    float literAmount;
    int miles;
    char color[20]; // this too
} DATA;

扫描/打印:

if(fscanf(fp, "%s %f %d %s\n", data[counter].type, &data[counter].literAmount, &data[counter].miles, data[counter].color) !=4) {
    return -1;
} 
printf("data stored is: %s %f %d %s\n", data[counter].type, data[counter].literAmount, data[counter].miles, data[counter].color);

然而,这有一个很大的问题 - 你可能会在20个字符的数组中读取超过20个字符,这是非常糟糕的。

请参阅本网站上的other questions了解如何处理该问题。