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

編集:これは私がそれをやり直した後今持っているものです、私は次のコンパイルエラーを受け取ります:参照の配列としての「武器」の宣言; 関数'voiddisplayfile(...)'で、'fin'はこのスコープで宣言されていません。'weapon'はこのスコープでは宣言されていません。ma、e'i'のルックアップが'scoping[-fpermissive]のISO'に変更されました。

4

3 に答える 3

1

私は最初にstd::stringchar配列よりも使用する傾向があります-それらは操作が簡単です。したがって、構造は次のようになります。

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ここで2つ、stringからintへの変換関数として使用しました。atoiが使用されstrtolますが、ここで実装する必要はありませんが、柔軟性がわずかに向上し、エラーチェックが向上します。ここでは別の選択肢があったstringstreamかもしれません。

次に、名前が空かどうかを示すブール値を返します。これは、コードの後半でeof()ifstreamをチェックするときに、ファイルの終わりを超えて読み取るまで実際には設定されないためです。したがって、最後の良い読み取りはそれを設定しませんが、それを超えてリードする最初の試みは設定します。ここでfalseを返すと、ifstreamがファイルの最後にあるために「get」が失敗したことが呼び出し元に示されます。

最後に、次のすべての武器を読み取るための何かが必要です。

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「空の」武器の追加を防ぐことができなかった場合は、ブレークの呼び出しに注意してください。最も魅力的な解決策ではありませんが、機能するはずです。

于 2012-05-24T10:41:20.150 に答える
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.
于 2012-05-24T10:21:24.520 に答える
-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();
}
于 2012-05-24T10:15:46.277 に答える