将 txt 文件读入结构并显示数组
Reading a txt file into a struct and displaying the array
我无法从文本文件中获取信息并将其放入我创建的结构中,然后显示数组。 到目前为止,我创建了结构并创建了带有参数的函数,但它似乎不起作用。如果你能引导我朝着正确的方向前进,那就太好了。
#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;
}
这是我的数据2.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)
相关文章:
- 显示数组中的学生 ID 和最高分
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 使用指针显示数组的值
- 我无法显示包含整数最大值的索引. 但我可以显示数组中整数的最高值
- 数组:如何在指定索引之前显示数组的第一个和最后一个元素以及数组元素的差异?
- 使用增量/减少操作员在C 中显示数组的奇怪结果
- 将25个甚至整数存储到一个名为intlist的整数阵列中,然后在屏幕上显示数组,并在屏幕上显示名为fivintegers
- 显示数组的问题.一切都放在一个单独的行中,但不放入单独的列中
- strlen 显示数组元素的数量错误
- 我的函数不会用" "替换".",也不会按预期显示数组
- 以C++为单位乘以并显示数组的值
- 找不到标识符,尝试在 C++ 中显示数组值
- 如何显示数组名称中的名称
- 在列表小部件 Qt C++中显示数组内容
- c++将.txt文件中的字符串添加到字符串数组中,并显示数组的值
- C++如何将数组传递给函数,然后调用它来显示数组的值
- 从函数更新(覆盖)和显示数组
- 在单独的数据中显示数组数据
- 仅显示数组文本文件中的第一个字母