我正在尝试从名为“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]。