【问题标题】:Writing a file to a struct array将文件写入结构数组
【发布时间】:2013-12-09 05:50:46
【问题描述】:

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

这是函数头

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;
}

【问题讨论】:

  • 将文本文件的内容读入结构似乎不是您的主要问题。如果您的课程没有涉及动态内存分配,并且作业没有指定文件可能包含多少条记录,您将不得不向您的教授寻求澄清。

标签: c arrays file struct records


【解决方案1】:

大概如下:

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2016-01-26
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多