.txt文件中的结构数组
Array of Structs from .txt file
有一个项目,我应该要求用户输入一个文件名,然后获取该文件并生成一个结构数组。我完全迷路了!这是我迄今为止拥有的
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string filename;
ifstream inFile;
struct ftballPlayer
{
int NO;
string first;
string last;
char POS;
char clas;
int height;
char ds;
int iheight;
string hometown;
};
int counter=0;
const int MAX_PLAYER=50;
void printList(const ftballPlayer list[], int listSize);
void printOne ( ftballPlayer two);
void getData(ifstream& inFile, ftballPlayer list[], int& listSize);
int main ()
{
ftballPlayer list[MAX_PLAYER] ;
cout << "Enter the name of the input file: " ;
cin >> filename;
inFile.open(filename.c_str () );
if (!inFile)
{
cout<<"Cannot Open Input File."<<endl;
cout<< "Program Terminates!"<<endl;
return 1;
}
inFile.ignore (200,'n');
getData (inFile, list, counter);
for ( counter = 0;counter < 50;counter++)
{
printOne (list[ counter] ) ;
cout <<endl;
}
return 0;
}
void getData(ifstream& inFile, ftballPlayer list[], int& listSize)
{
ftballPlayer item ;
listSize = 0;
inFile >> item.NO >> item.first >> item.last
>> item.POS >> item.clas >> item.height
>> item.ds >> item.iheight >> item.hometown;
while (inFile )
{
list [listSize ] = item ;
listSize++;
inFile >> item.NO >> item.first >> item.last
>> item.POS >> item.clas >> item.height
>> item.ds >> item.iheight >> item.hometown;
}
inFile.close () ;
}
void printList(const ftballPlayer list[], int listSize)
{
int looper;
for ( looper = 0; looper < listSize ; looper++)
{
printOne ( list [looper] );
cout << endl ;
}
}
void printOne ( ftballPlayer one)
{
cout << fixed << showpoint << setprecision (2);
cout << "NO " << one.NO;
cout << setw(5) << left << " Name: " << one.first << " " << one.last;
cout << " POS " << one.POS << setw(5);
cout << "Class "<<one.clas<<setw (5);
cout << "Height "<<one.height<<" "<<one.ds<<" "<<one.iheight<<setw(5);
cout << "Hometown " << one.hometown << endl;
}
有人能告诉我我是否走上了正轨吗?我得到的打印结果甚至不接近文本文件,也就是这个。
没有名字POS类身高体重家乡/高中/最后一所大学
60 Josh Mann OL SO 6-4 300弗吉尼亚州弗吉尼亚海滩/海洋湖
64 Ricky Segers K/p FR 5-11 185弗吉尼亚州格伦艾伦/亨利科
70 Brandon Carr OL RS_SR 6-2 305弗吉尼亚州切萨皮克/西部分校/福克联合军事学院
53 Calvert Cook LB FR 6-0 250弗吉尼亚州诺福克/Booker T。华盛顿
51 Michael Colbert DE RS_SR 6-1 230 Fayetteville,N.C./E.E.Smith
22 T.J.Cowart CB RS_JR 5-9 190弗吉尼亚海滩/海洋湖
1 Jakwail Bailey WR SO 5-11 185新泽西州哈登菲尔德/保罗六世
25 Andre Simmons S JR 6-0 205洛顿,弗吉尼亚州/南县/范德比尔特
34 Johnel Anderson RB FR 5-8 180 Sicklerville,N.J./保罗六世
这是用户可以输入的三种信息之一,但它们都具有相同类型的信息。我查阅了我的课本,在网上搜索了几个小时,当用户输入文件而不是从一个文件开始时,我找不到任何关于这样做的信息。如有任何帮助或指示,我们将不胜感激。
我马上发现了一些错误。
struct ftballPlayer
{
int NO;
string first;
string last;
char POS; // Should be multiple characters
char clas; // Should be multiple characters
int height;
char ds;
int iheight;
string hometown;
};
我怀疑,如果你的POS/Class尺寸合适,效果会更好。为什么不把它们串起来呢?
此外,阅读最后一部分也有一些问题。看起来你必须寻找/来区分不同的部分,我不认为你会这么做。
总的来说,如果你能改变输入的格式,读这篇文章会干净得多,但我怀疑你做不到。逗号分隔的值很容易读取,其他除法器也是如此。
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组