c++ - 将文本文件读入结构并显示数组

标签 c++ arrays struct compiler-errors

我在从文本文件中获取信息并将其放入我创建的结构中,然后显示数组时遇到了麻烦。到目前为止,我已经创建了结构并使用参数创建了函数,但是它似乎没有用。如果您能引导我朝正确的方向发展,那就太好了。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int N=5;

struct RECORD
{
    char *Name;
    int Age;
    float Gpa;
};

void CopyRecords (string filename, int p[N]);
void Display (RECORD x[]);

int main()
{
    RECORD p[N];

    //Read data from this file into array p

    CopyRecords("data2.txt", p);

    //Display all records
    Display(p);

    //Terminate Program
    system ("pause");
    return 0;
}
void CopyRecords (string filename, int p[N])
{
    ifstream f;
    f.open(filename, ios::in);

    for(int i = 0; i <= N; ++i)
    {
        f >> p[i];
    }
}
void Display (RECORD x[])
{
    for (int i = 0; i < N; ++i)
        cout << x[i].Name << " " << x[i].Age << " " << x[i].Gpa << endl;
}

这是我的data2.txt
Martin Smith/ 22 2.2
Austin Clinton/ 18 3.1
Johnson/ 19 2.9
Maggie Jones/ 23 2.3
Tyler W Brown/ 16 3.4

最佳答案

建议使用i < N而不是i <= N中的for(int i = 0; i <= N; ++i)

关于c++ - 将文本文件读入结构并显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352920/

相关文章:

C++ 对象模型转换?

c++ - 具有集体功能的 MPI 死锁

c++ - 谷歌模拟 : Is it ok to use global mock objects?

c - 如何将结构成员数组传递给C中的函数

c# - 为值类型实现 operator++ 的正确方法是什么?

c++ - FileSink、StringSink、Filesource、StringSource Crypto++ 之间有什么区别

java - 在 Java 运行时从数组中删除元素

javascript - 如何在 JavaScript 中将两个依赖数组缩减为一个对象?

java - 循环并不能处理所有事情

c - 分配类型不兼容,从 BSTNode* 到 BSTNode*