将文件写入结构数组

时间:2013-12-09 05:50:47

标签: c arrays file struct records

任何人都知道如何将文本文件读入结构数组?我一直试图弄清楚如何做到无济于事。

这是函数头

int getRawData(FILE* fp, struct nameRecord records[], int currSize)

其中第一个参数传递已经打开以供读取的文件,第二个参数传递一个nameRecord结构数组,第三个参数传递给该数组中当前的记录数。该函数应该将文件中的数据读入数组,并将其放在数组的末尾。然后在读取文件后返回数组中的记录总数。

我在初始化nameRecord结构数组的元素数时也很茫然。我们从来没有被教过内存分配,问题也没有提到文件中有多少条记录,使初始化成为一种令人沮丧的练习。到目前为止,我正在接受另一个论坛的人和使用malloc的建议,但我甚至不知道它的作用。

关于程序本身提供上下文的一些信息:

  

程序会要求用户输入一个名字(你可能会认为是   名称长度不超过30个字符)。然后它会找到   该名称在1921年至2010年间流行并打印出图表和图表   图形。然后程序将询问用户是否希望再做另一个   分析并重复这个过程。

     

该程序将从以下数据源中提取信息   确定名称的受欢迎程度。

     

安大略女宝宝名字安大略男宝宝的名字

     

请注意,有些名字被认为是男性和女性,所以你的   无论名称如何,程序都需要来自两个文件的数据   输入

我对这个功能的尝试:

//function that reads and places the read files into the struct arrays
    int getRawData(FILE* fp, struct nameRecord records[], int currSize) {
        int i;
        for(i = 0; i < currSize; i++) {
            fscanf(fp, "%[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency);
        }

这是整个计划:

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

struct nameRecord {
    char name[31];
    int year;
    int frequency;
};
void allCaps(char[]);
int getRawData(FILE*, struct nameRecord[], int);
void setYearTotals(struct nameRecord, int, int);
void setNameYearTotals(char, struct nameRecord, int, int);
void getPerHundredThousand(int, int, double);
void printData(double);
void graphPerHundredThousand(double);

int main(void)
{
    int currSizem = 0;
    int currSizef = 0;
    struct nameRecord *records;
    FILE* fp = NULL;
    FILE* fp2 = NULL;
    char name[31];
    printf("Please enter your name: ");
    scanf("%30[^\n]", name);
    printf("your name is %s\n", name);

//opening both male and female name files and reading them in order to get the total number of records in the array
    fp = fopen("malebabynames.csv", "r");
    if (fp != NULL) {
        printf("file opened\n");
        while(3 == fscanf(fp, "%[^,],%d,%d", records[currSizem].name, &records[currSizem].year, &records[currSizem].frequency)) {
            currSizem++;
        }
    } else {
        printf("file failed to open\n");
    }
    if(currSizem > 0) {
        records = malloc(currSizem * sizeof(struct nameRecord));
    }

    fp2 = fopen("femalebabynames.csv", "r");
    if (fp != NULL) {
        printf("file opened\n");
        while(3 == fscanf(fp2, "%[^,],%d,%d", records[currSizef].name, &records[currSizef].year, &records[currSizef].frequency)) {
            currSizef++;
        }
    } else {
        printf("file failed to open\n");
    }
    if(currSizef > 0) {
        records = malloc(currSizef * sizeof(struct nameRecord));
    }

    return 0;
}

//function that automatically capitalizes the users inputted name
void allCaps(char s[]) {
    while(*s != '\0') {
        *s = toupper((unsigned char) *s);
        s++;
    }
}
//function that reads and places the read files into the struct arrays
int getRawData(FILE* fp, struct nameRecord records[], int currSize) {
    int i;
    for(i = 0; i < currSize; i++) {
        fscanf(fp, "%[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

大致如下:

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

struct nameRecord {
    char name[31];
    int year;
    int frequency;
};

int getRawData(FILE*, struct nameRecord[], int);

int main(void){
    const char *MaleFilePath =   "malebabynames.csv";
    const char *FemaleFilePath = "femalebabynames.csv";
    int currSizem = 0;
    int currSizef = 0;
    FILE* mfp = NULL;
    FILE* ffp = NULL;
    struct nameRecord *recordsOfMale, *recordsOfFemale;

//opening both male and female name files and reading them in order to get the total number of records in the array
    mfp = fopen(MaleFilePath, "r");
    if (mfp != NULL) {
        int dummy;
        printf("file opened\n");
        //line count
        while(1 == fscanf(mfp, " %*[^,],%*d,%d", &dummy)){
            ++currSizem;
        }
    } else {
        printf("file(%s) failed to open\n", MaleFilePath);
        exit(EXIT_FAILURE);
    }
    if(currSizem > 0) {
        recordsOfMale = malloc(currSizem * sizeof(struct nameRecord));
        if(recordsOfMale == NULL){
            perror("malloc for Male records");
            exit(EXIT_FAILURE);
        }
    }
    rewind(mfp);
    if(currSizem != getRawData(mfp, recordsOfMale, currSizem)){
        fprintf(stderr, "I could not read a record for the specified number\n"
                        "at reading %s\n", MaleFilePath);
        exit(EXIT_FAILURE);
    }
    fclose(mfp);

    //Do something

    free(recordsOfMale);

    return 0;
}

//function that reads and places the read files into the struct arrays
int getRawData(FILE* fp, struct nameRecord records[], int currSize) {
    int i;
    for(i = 0; i < currSize; i++) {
        if(3!=fscanf(fp, " %[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency))
            break;
    }
    return i;
}