【发布时间】:2012-05-24 10:00:13
【问题描述】:
我正在尝试从一个名为“weapon.txt”的文件中读取这些行并将它们输入到一个结构中,该结构的行数很长
struct weapon
{
char name[20]; //Edited
int strength;
}
要读取的文件如下所示:
Excalibur
150
Throwing Stars
15
Rapier
200
Bow and Arrow
100
Axe
200
Crossbow
100
Scimitar
250
Rusted Sword
10
Soul Slayer
500
我现在的代码是
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;
struct WeaponInfo
{
char name[16];
int strength;
};
const int MaxWeap = 10;
void openfile(ifstream&); //Opening the file
void displayfile(ifstream&, WeaponInfo&);//Display file
int main ()
{
WeaponInfo weapon[MaxWeap];
ifstream fin;
openfile(fin);
displayfile(fin, weapon[MaxWeap]);
}
void openfile(ifstream& fin)
{
fin.open("weapon.txt");
}
void displayfile(ifstream& fin, WeaponInfo& weapon[MaxWeap])
{
char nm;
int str;
while (fin.eof() == 0)
{
for(int i = 0; i <= MaxWeap; i++);
{
fin.getline(nm);
fin.getline(str);
strcpy(weapon[i].name, nm);
strcpy(weapon[i].strength, str);
i++;
cout << weapon[i].name << "\n" << weapon[i].strength << endl;
}
}
fin.close();
}
编辑:这就是我现在在重新做之后所拥有的,我收到以下编译错误:将“武器”声明为引用数组;在函数 'void displayfile(...) 'fin' 未在此范围内声明; 'weapon' 未在此范围内声明; ma,e 查找 'i' 已更改为 ISO 'for' 范围 [-fpermissive]。
【问题讨论】:
-
好的。那么你的问题是什么?
-
@OliCharlesworth 我的问题很好,我该怎么做呢?很抱歉在我最初的陈述中没有说清楚。
-
@Lap 对于只是寻求帮助的人来说,这有点讽刺,就像我刚刚评论了另一个答案我不是在寻找 cntrl c/v,我实际上是在试图理解怎么办……
标签: c++ file data-structures input ifstream