将文件输入到结构中 [英] Inputting a file into a structure
问题描述
我试图从一个名为'weapon.txt'的文件中读取行,并将它们输入到这个
I am trying to read the lines from a file called 'weapon.txt' and input them into a structure something a long the lines of this
struct weapon
{
char name[20]; //Edited
int strength;
}
要读取的文件如下所示:
The file to be read looks like this:
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'未在此范围中声明; 武器不在本范围内声明;
This is what I have right now after re-doing it, I am getting compile errors of : declaration of 'weapon' as array of references; In function 'void displayfile(...) 'fin' was not declared in this scope; 'weapon' is not declared in this scope; ma,e lookup of 'i' changed for ISO 'for' scoping [-fpermissive].
推荐答案
我首先倾向于使用 std :: string
而不是char数组 - 它们只是更容易使用。所以结构noww看起来像这样:
I'd firstly tend to use std::string
rather than char arrays - they're just easier to work with. So the structure noww looks like this:
struct weapon
{
string name;
int strength;
};
接下来你需要一些东西来从输入流中读取结构:
Next you need something that will read the structure from an input stream:
bool getWeapon( ifstream& is, weapon& w )
{
getline(is, w.name) ;
string strengthStr;
getline(is, strengthStr) ;
w.strength = strtol( strengthStr.c_str(), NULL, 0 );
return !is.eof();
}
作为从string到int的转换函数。 atoi
,但 strtol
为您提供稍微更多的灵活性,关键是更好的错误检查,虽然我没有打扰在这里实现它。 stringstream
可能是另一种选择。
Two things here, I've used strtol
as a conversion function from string to int. atoi
is used but strtol
gives you slightly more flexibility and crucially, better error cchecking, alkthough I've not bothered to implement it here. A stringstream
might have been another alternative here.
其次,我返回一个布尔值,这样做的原因是,当在代码的后面,我检查ifstream上的 eof()
,它实际上并没有设置,直到你读过去的结束文件。所以最后好的读不会设置它,但第一次尝试reead过去它会。在这里返回false将告诉调用者'get'失败,因为ifstream在文件结束。
Secondly, I return a boolean indicating whether the name was empty. The reason for this is that when, later in the code, I check for eof()
on the ifstream, it isn't actually set until you read past the end of the file. So the last good read will not set it but the first attempt to reead past it will. Returning false here then will indicate to the caller that the 'get' failed due to the ifstream being at end of file.
最后,我们需要一些东西来读取所有的weappons in:
Lastly, we need something to read all of the weappons in:
ifstream input;
input.open("weapons.txt");
vector<weapon> ws;
if ( input )
{
while (! (input.eof()))
{
weapon w;
if ( ! getWeapon( input, w ) )
break;
ws.push_back( w );
}
}
input.close();
这将把所有的武器放入一个矢量。注意,如果调用 getWeapon
无法阻止添加空武器,则会调用。不是最迷人的解决方案,但它应该工作。
This wwill place all the weapons into a vector. Note the call to getWeapon
breaks if it failed to prrevent adding on an 'empty' weapon. Not the most glamorous solution but it should work.
这篇关于将文件输入到结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!