我试图读取一个叫“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();
}
编辑:这是我有权现在重做之后,我收到了编译错误:声明'weapon'为引用数组;在函数中'void displayfile(...)'fin'未在此范围内声明; '武器'没有在这个范围内声明;我对'我'的查找改为ISO'查看'[-fpermissive]。
好的。那么你的问题是什么? –
@OliCharlesworth我的问题很好,我究竟该怎么做呢?对不起,我的初步声明不清楚。 – Joey
@Lap对于一个正在寻找帮助的人来说,这是一个讽刺评论,就像我刚才评论过的另一个答案,我不是在寻找一个cntrl c/v,我实际上试图了解如何做... – Joey