2016-10-21 43 views
0

まず、私がしようとしていることを説明したいと思います。だから、私は彼らのポイントに続いて10の名前でテキストファイルを持っています。私は、構造体を作成し、関数(void)を使ってファイルから情報を読み出そうとしています。そして、それは私のコードやテキストファイルです:テキストファイルから構造体への読み込み

コード:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

void read(struct laLiga t[]) { 
    ifstream infile("info.txt"); 
    int i = 0; 
    while(infile >> t.team[i] >> t.points[i]) { 
     infile.get(t.team); 
     infile >> t.points; 
     i++; 
    } 
} 

struct laLiga { 
    char team[50]; 
    int points; 
}; 

int main() { 
    struct laLiga t[10]; 
    read(t); 
    return 0; 
} 

テキストファイル:すべての

Athletic Bilbao 15 
Atletico Madrid 18 
Barcelona 16 
Alaves 10 
Las Palmas 12 
Real Madrid 18 
Real Sociedad 10 
Sevilla 17 
Eibar 11 
Villarreal 16 
+0

あなたの質問は何ですか? – Michiel

答えて

1

あなたは物事を簡単にするためにstd::vectorを使用することができます。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <sstream> 
#include <iomanip> 

using namespace std; 

auto file_text = 
R"(Athletic Bilbao 15 
Atletico Madrid 18 
Barcelona 16 
Alaves 10 
Las Palmas 12 
Real Madrid 18 
Real Sociedad 10 
Sevilla 17 
Eibar 11 
Villarreal 16)"; 

struct laLiga { 
    std::string team; 
    int points; 
}; 

void read(std::vector<laLiga>& t) { 
    //ifstream infile("info.txt"); // would also work 
    std::stringstream infile{file_text}; 
    while(infile) { 
     laLiga laliga{}; 
     std::string partial_team_name{}; 
     while(!(infile >> laliga.points)) { // keep going until we get the points 
      infile.clear(); // we could not read points, infile stream now in error; clearing to continue 
      if(infile >> partial_team_name) // maybe the next string is part of a team name 
       laliga.team += (laliga.team.empty() ? "" : " ") + partial_team_name; // add to team name with space if there was something already in there 
      else 
       return; // no points, no team, we're done 
     } 
     t.push_back(laliga); // after we get the points we can add the team 
    } 
} 

int main() { 
    std::vector<laLiga> t; 
    read(t); 
    for(auto i : t) { 
     std::cout << std::setw(30) << i.team; 
     std::cout << std::setw(10) << i.points << '\n'; 
    } 
    return 0; 
} 

出力:

 Athletic Bilbao  15 
     Atletico Madrid  18 
      Barcelona  16 
       Alaves  10 
      Las Palmas  12 
      Real Madrid  18 
     Real Sociedad  10 
       Sevilla  17 
       Eibar  11 
      Villarreal  16 

live demo

+0

こんにちは、良い提案に感謝、私はベクトルについて話しています。すべてが良く見えますが、私はもう少し説明が必要です。まず第一に、私はこの条件がどのように働くか理解していません: "!(infile >> laliga.points)"第二に、私はファイルをクリアする必要があり、何が変更されるのか理解できません。最後に、これがどのように機能するか説明できますか? 'laliga.team.empty()? "": "" '' '' –

+0

'!(infile >> laliga.points)'は 'infile'から' points'への読み込みを意味します。 'infile >> laliga.points'が失敗すると' false'と評価されます。ポイントを読み取れない場合は '!(infile >> laliga.points)'が 'true'になりますので、プログラムは引き続きチーム名の一部を読み込み続けます。 – wally

+0

'infile.clear();'フラグを除いて 'infile'は変更されません。フラグはクリアされます。 'infile >> laliga.points'が失敗した(ポイントを読み取ることができなかった)後、再度読み込むために' infile'のエラーフラグをクリアする必要があります。 – wally

3

まず、あなたがreadでそれを使用前に構造を定義する必要があります関数。

第2に、関数では、変数tがそのメンバではなく配列です。したがって、t[i].teamt[i].pointsを使用する必要があります。

第3に、ループ状態の>>演算子を使用してファイルからデータを読み込んでいます。ループ内でをもう一度に読んではいけません。

第4に、読みたい入力ファイルは実際には思ったよりも解析が難しいです。これは、の両方にスペースを入れずに名前を付けることができるからです。入力ストリームのget関数と入力演算子>>の両方がスペースで区切られます。おそらくread the whole lineにして、それを最後のスペースで分割する必要があります。

最後に、そして実際には理由は私がではないあなたは実際に質問されていないか、あなたのコードに何が間違っているかを教えてください。今後の質問については、read about how to ask good questionsをご記入ください。

+0

ありがとうございました。質問を投稿する前に重要な情報を読んでいないのは残念です。 –

0

あなたは次のようになり、その後、最良のテキストファイル内の構造体を読み書きしたい場合ifstreamread()機能とofstreamwrite()機能を使用する。使用方法は次のとおりです。どちらもread()write()関数の最初のパラメータは、我々はバイトの配列と完全な構造体をキャストするreinterpret_castを使用して、ファイルに書き込みをしている理由char*でなければなりません

#include <iostream> 
#include <fstream> 
#include <cstring> // for strncpy() 

using namespace std; 

struct laLiga { 
    char team[50]; 
    int points; 
}; 

int main() 
{ 
struct laLiga t; 

strncpy(t.team,"Some Name",sizeof("Some Name")); 
t.points = 100; 

//do this for writing a complete struct to a text file at once 
ofstream output("info.txt"); 
output.write(reinterpret_cast<char*>(&t),sizeof(t)); 
output.close(); 

//do this for reading a complete struct from a text file at once 

struct laLiga T; 

ifstream input("info.txt"); 
input.read(reinterpret_cast<char*>(&T),sizeof(T)); 
input.close(); 

//now T will hold both the name and points fields from the text file without doing it individually 

cout << "Name = " <<T.team << endl; 
cout << "Points = " << T.points << endl; 

return 0; 
} 

OUTPUT(コンソール)

Name = Some Name 

    Points = 100 

。 このようにすることの欠点は、通常のテキストではないため、テキストファイルをWindowsで開いたときに読み込み不能になることですが、読み書きははるかに簡単です。