c - 将文件写入结构体数组

标签 c arrays file struct records

有人知道如何将文本文件读入结构数组吗?我一直在尝试弄清楚如何做到这一点,但没有成功。

这是函数头

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

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

我在初始化 nameRecord 结构数组的元素数量时也不知所措。我们从未被教导过内存分配,并且问题没有提及文件中有多少记录,这使得初始化成为一种令人沮丧的练习。到目前为止,我正在听取另一个论坛上某人的建议并使用 malloc,但我什至不知道它的作用。

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

program will ask the user to enter a name (you may assume that the name will be no more than 30 characters long). It will then find the popularity of the name between 1921 and 2010 and print out a chart and graph. The program will then ask the user if they wish to do another analysis and repeat the process.

The program will pull information from the following data sources in determining the popularity of a name.

ontario female baby names ontario male baby names

Note that some names are considered both male and female so your program will needs the data from both files regardless of the name entered.

我对该功能的尝试:

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

最佳答案

大致如下:

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

关于c - 将文件写入结构体数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464001/

上一篇:C文件作业

下一篇:c - 我需要光标落下

相关文章:

c - 数组中的输出崩溃

objective-c - Objective C 语法查询

java - 在 JNI 调用后尝试过快地访问数组时出现段错误

javascript - 如何将 Markdown 的一小部分解析为 React 组件?

java - 如何使用java存储从php文件检索的数据

php - 存储在数组中并插入不同的字段名称

python - 读取文件并打印前两行和最后两行的脚本

c - 为什么这个排序功能不起作用? C

Java 文件分割

file - 在 gnuplot 中读取外部数据文件