2012-05-24 61 views
0

我试图读取一个叫“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]。

+1

好的。那么你的问题是什么? –

+0

@OliCharlesworth我的问题很好,我究竟该怎么做呢?对不起,我的初步声明不清楚。 – Joey

+0

@Lap对于一个正在寻找帮助的人来说,这是一个讽刺评论,就像我刚才评论过的另一个答案,我不是在寻找一个cntrl c/v,我实际上试图了解如何做... – Joey

回答

-1

假设你控制的是weapons.txt文件,不要去检查文件中的错误,你可以这样做。接下来的时间,做一个小小的研究... :)

#include <fstream> 
#include <vector> 
#include <string> 
#include <iostream> 
#include <cstdlib> 

using namespace std; 

struct weapon 
{ 
string name; 
int strength; 
weapon(string n, int s) : name(n), strength(s) {} 
}; 

void readFileToVec(vector<weapon> &myVec) { 
    ifstream in("weapon.txt"); 
    while (!in.eof()) { 
     string name; 
     getline(in,name); 
     string strength; 
     getline(in,strength); 
     weapon myWep(name,atoi(strength.c_str())); 
     myVec.push_back(myWep); 
    } 
    in.close(); 
} 
+0

我确实拥有文件的控制权,我花了一个小时试图研究的最佳部分,但我认为我的问题在于我无法使用#include 。 – Joey

+0

为什么呢? – Martol1ni

+0

由于讲师提出的限制,并且在您可能认为我只是想找到一个简单的cntrl + c/v答案之前,这是部分其他代码所需的,我只是无法弄清楚如何得到它去工作... – Joey

0

伪代码是这样的,(等Martol1ni已编码的你):

open the file 
while (!end-of file) 
{ 
    create instance of struct weapon 
    read a line and strcpy into weapon.name 
    read a line and set weapon.strength = atoi(line) 
    do something with the instance, eg. add to list, call a member function, etc. 
} 
loop 
close file. 
+0

这看起来真的很有帮助谢谢,稍后我会怎么回答:) – Joey

1

我会先倾向于使用std::string而不是char数组 - 它们更容易使用。所以结构noww看起来是这样的:

struct weapon 
{ 
    string name; 
    int strength; 
}; 

接下来,你需要的东西,会从输入流中读取的结构:这里

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(); 
} 

两件事情,我已经使用strtol从一个转换功能字符串转换为int。 atoi被使用,但strtol给你稍微更多的灵活性和关键,更好的错误检查,尽管我没有打扰到在这里实现它。 A stringstream可能是另一种选择。

其次,我返回一个布尔值,指示名称是否为空。其原因是,在代码后面,我检查ifstream上的eof(),直到读完文件结尾才真正设置它。所以最后的好阅读不会设置它,但第一次尝试reead过去会。如果在这里返回false,则会向调用者指示由于ifstream处于文件结尾而导致'get'失败。

最后,我们需要一些东西来阅读所有的weappons的:

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(); 

这wwill所有的武器放到一个载体。请注意,如果getWeapon未能通过“空”武器进行加入,则会致电getWeapon。不是最迷人的解决方案,但它应该工作。

相关问题