2012-05-24 60 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。不是最迷人的解決方案,但它應該工作。

相關問題