C:难以理解这个基于文件/结构的指令

标签 c arrays file struct record

我正在尝试创建一个程序,要求用户输入名称(假设每个名称的长度为 30 个字符或更少)。然后,它会找到该名称在 1921 年至 2010 年间的受欢迎程度,并打印出图表。然后,程序将询问用户是否愿意进行另一次分析并重复该过程。

该程序需要使用大约 7 个不同的函数,但我无法理解我需要使用的其中 2 个函数。

函数1:

int getRawData(FILE* fp,struct NameRecord records[],int currSize);`

function is passed a pointer to a file that is already open for reading. Each line in this file will be of the form (the names are fully capitalized in the file):

year,NAME,frequency

The function is also passed an array of NameRecord structs (records) and the number of records currently in that array (currSize). this function will read the data from the file into the array placing it at the end of the array (first record from file will be placed into array[currSize]). Function will return total number of records in the array after reading in the file.

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

现在我不确定这是否意味着我需要在从文件接收记录的函数中创建一个新数组,或者它是否完全是其他东西,并且我没有正确读取指令。(文件中本例为malebabynames.csv)

第二个功能:

void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[]);

function is passed an array of NameRecords (records) and the size of that array (size). It is also passed an array called yearRangeTotal which will be used to store the total population for a given year range.

yearRangeTotal[0] holds the total population between 1921 to 1925
yearRangeTotal[1] holds the total population between 1926 to 1930
yearRangeTotal[2] holds the total population between 1931 to 1935
...
yearRangeTotal[17] holds the total population between 2006 to 2010

最佳答案

(1) 不需要,只需填写传递的数组即可。这与传递 fread 一个 char(或其他任何内容)数组并要求它读取 N 个项目没有什么不同。检查返回码以了解实际读取了多少个。

(2) 不太清楚。我猜想您会被要求迭代 NameRecord 数组并根据 NameRecord.year 中的“year”字段增加适当的yearRangeTotal 元素。

关于C:难以理解这个基于文件/结构的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450066/

相关文章:

c++ - 无法在继承进程中读取文件

c++ - 打开目录中的文件夹

c - 运行我的 C 程序时在 Linux 中出现段错误

c - 使用 strtok [C] 程序崩溃

c - 在c中标记一个字符串

c++ - 错误: 'ALIGN' undeclared (first use in this function) with ALIGN defined into macro

C:子范围内的反向数组

arrays - 如何声明将与嵌套对象一起使用的模型?

java - 使用方法和多个类的一副纸牌

javascript - 如何根据单词作为键分隔符读取文本文件的 block ?