我試圖讀取一個叫「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