2017-11-20 5 views
0

TXT INFILEから変数情報を割り当てる:これは私のテキストファイルであるC++

12345 shoe 5 0 
34534 foot 72 1 
34562 race 10 0 
34672 chicken 24 150 
88 pop 65 0 

私はこのファイルを取得し、行ごとを行く、識別子itemNumとして最初の番号を割り当て、第二する必要があります単語はitemName、第3の数字はitemPrice、最後の数字はitemAdjusmentValueです。最後の2つの数字であるitemPriceitemAdjusmentValueを使って算術演算を行う必要があります。これまで

コード:文

'>>' の前にトークンのプライマリ表現を期待しながら、

using namespace std; 

// making a struct to store each value of the cuadre 

struct Cuadre 
{ 

    int itemNum; 
    string itemName; 
    int itemPrice; 
    int itemAdjusment; 

}; 

int main(){ 

    ifstream infile("sheet_1.txt"); 
    string checkLine; 

    if (infile.is_open()){ 

     while (infile.good()){ 

      getline (infile, checkLine); 
      cout << checkLine << endl; 
     } 
    } 
    else 
     cout << "error with name of file" << endl; 


    vector<Cuadre> procedures; 

    Cuadre line; 

    while(Cuadre >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment){ 
     procedures.push_back(line); 
    } 

このコードは

私は本当にカント最後にエラーが発生しますこれを行う方法に関する具体的なチュートリアルを見つけると、私は良い量を見てきました。

+2

「checkLine >>」ではありませんか? – Barmar

+1

そして、ファイルから読み込むループの内側にあるはずです。それ以外の場合は、最後の行を処理しているだけです。 – Barmar

+0

@JonathanWakely既に私の答えでそれを修正しました。 – Barmar

答えて

1

投稿したコード(>>istream operatorsを参照)から、あなたのファイルから読み込まれた文字列データの内容を構造体のメンバーにストリーミングするように見えます。

ストリングがストリーミングインターフェースを提供しないため、std::string(例:checkLine >> x >> y >> z)から直接ストリーミングすることはできません。

ストリームを使用するには、std::stringstreamなどのストリームを使用する必要があります。 あなたの文字列checkLineに移入した後、あなたのデータメンバにそのからストリーミングでき

std::stringstream ss(checkLine); 
ss >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment; 

コード例:あなたが読みループ内でベクターにプッシュする必要があり

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

using namespace std; 

// making a struct to store each value of the cuadre 

struct Cuadre 
{ 

    int itemNum; 
    string itemName; 
    int itemPrice; 
    int itemAdjusment; 

}; 

int main(){ 

    ifstream infile("sheet_1.txt"); 
    string checkLine; 

    vector<Cuadre> procedures; 

    if (infile.is_open()){ 

     while (infile.good()){ 

      getline (infile, checkLine); 
      cout << checkLine << endl; 

      Cuadre line; 
      std::stringstream ss(checkLine); 
      ss >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment; 
      procedures.push_back(line); 
     } 
    } 
    else 
     cout << "error with name of file" << endl; 

    return 0; 
} 
+0

別の質問を投稿する必要があるという回答を得るために、私は –

+1

@TentacleSlingerを見つけました。 SOはフォーラムではなく、1つの質問ごとのスタイルのサイト –

+0

私はちょうどそれを考え出して削除する方法を見つけようとしています。 –

1

ファイルから。そして、checkLineからCuadreまでのフィールドをタイプ名にする必要があります。読み込み可能な変数ではありません。しかし、それを行うにはstringstreamを作成する必要があります。

int main(){ 

    ifstream infile("sheet_1.txt"); 
    string checkLine; 
    vector<Cuadre> procedures; 

    if (infile.is_open()){ 

     while (getline (infile, checkLine)){ 
      Cuadre line; 
      cout << checkLine << endl; 
      stringstream linestream(checkline); 
      if (linestream >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment) { 
       procedures.push_back(line); 
      } else { 
       cout << "incorrect line" << endl; 
       break; 
      }     
     } 
    } 
    else { 
     cout << "error with name of file" << endl; 
    } 
} 

while (infile.good())それは本質的while (!infile.eof())と同じだ、また正しくありません。あなたにも構造体に直接読み込むしようとすることができWhy is iostream::eof inside a loop condition considered wrong?

+0

'std :: string'はストリーミングインターフェイスを提供しません。最初にストリングストリームを作成し、それを使って –

+0

という感謝をしてください。何らかの理由で、私はこのようなものが言語に追加されたと考えました(文字列を持つ抽出演算子を使用すると自動的にストリームが作成されます)。 – Barmar

1

を参照してください:

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

struct Cuadre 
{ 
    int itemNum; 
    std::string itemName; 
    int itemPrice; 
    int itemAdjusment; 
}; 

int main() 
{ 
    std::vector<Cuadre> procedures; 
    std::ifstream infile("sheet_1.txt"); 

    while (infile) 
    { 
     Cuadre line; 
     if (infile >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment) 
      procedures.push_back(line); 
     else 
      break; 
    } 

    if (!procedures.empty()) 
    { 
     for (auto &p : procedures) 
      std::cout 
      << "itemNum: " << p.itemNum << "\t" 
      << "itemName: " << p.itemName << "\t" 
      << "itemPrice: " << p.itemPrice << "\t" 
      << "itemAdjusment: " << p.itemAdjusment 
      << std::endl; 
    } 
    else 
     std::cout << "error with file or data" << std::endl; 

    return 0; 
} 

プリント:

itemNum: 12345 itemName: shoe itemPrice: 5 itemAdjusment: 0 
itemNum: 34534 itemName: foot itemPrice: 72 itemAdjusment: 1 
itemNum: 34562 itemName: race itemPrice: 10 itemAdjusment: 0 
itemNum: 34672 itemName: chicken  itemPrice: 24 itemAdjusment: 150 
itemNum: 88  itemName: pop itemPrice: 65 itemAdjusment: 0 
1

あなたがあなたのタイプCuadreため>>のオーバーロードを定義する場合は、あなたから直接読み取ることができますファイルをCuadreオブジェクトに追加します。

std::istream& operator>>(std::istream& is, Cuadre & cuadre) 
{ 
    std::string s; 
    getline(is, s); 
    std::cout << s; 
    std::stringstream ss(s); 
    ss >> cuadre.itemNum >> cuadre.itemName >> cuadre.itemPrice >> cuadre.itemAdjusment; 
    return is; 

    // or without logging 

    return is >> cuadre.itemNum >> cuadre.itemName >> cuadre.itemPrice >> cuadre.itemAdjusment; 
} 

int main(){ 

    ifstream infile("sheet_1.txt"); 
    vector<Cuadre> procedures; 

    for (Cuadre line; infile >> line;) { 
     procedures.push_back(line); 
    } 

    // or with #include <algorithm> 

    std::copy(std::istream_iterator<Cuadre>{ infile }, {}, std::back_inserter(procedures)); 
} 
+0

これは素晴らしいコードです。私が行う唯一の改善は、ローカルの 'Cuadre'(または4つのローカル変数)に' operator >> 'オーバーロードを読み込み、' cuadre'のメンバを設定して 'cuadre'パラメータがすべてのフィールドが正常に読み取れるかどうかを確認します。 –

関連する問題