2016-08-07 14 views
-2

私のDディレクトリから4つのテキストファイルを読み込み、文字列ベクトルに入れて、これらの文字列をベクトルから1つずつ取り出してユーザーに表示する簡単なコードを書きました。最初のファイルのみの内容を読み込み、他のファイルの内容は読み込まないという問題があります。私のDドライブにあり、事前ifstream forループ内の1つのテキストファイルを読み込みます

#include<iostream> 
#include<fstream> 
#include<string> 
#include<iomanip> 
#include<sstream> 
#include<vector> 
using namespace std; 

int main(){ 

    std::string line; 
    std::stringstream ss; 
    std::stringstream path; 
    std::vector<std::string> vectorOfStrings; 
    ifstream readFile; 


    for(int i=1; i<= 4; i++) 
    { 

    path << "D:\\Text0"<< i <<".txt"; 
    readFile.open(path.str()); 

    if(readFile.is_open()) 
    { 
     while(!readFile.eof()) 
     { 
      getline(readFile, line); 
      ss << line << "\n"; 

     } 
     ss<< "\n ***************************************\n"; 
     vectorOfStrings.push_back(ss.str()); 
     ss.str(""); 
     readFile.close(); 
     readFile.ignore(); 
     readFile.clear(); 
    } 
    } 


    ss << vectorOfStrings.at(0); 
    cout<<ss.str(); 
    ss.str(""); 

    ss << vectorOfStrings.at(1); 
    cout<<ss.str(); 
    ss.str(""); 

    ss << vectorOfStrings.at(2); 
    cout<<ss.str(); 
    ss.str(""); 

    ss << vectorOfStrings.at(3); 
    cout<<ss.str(); 
    ss.str(""); 

    return 0; 
} 

ファイルのおかげであなたが学ぶためにText01.txt、Text02.txt、Text03.txt、Text04.txt

+3

'ながら(readFile.eof(! ))Whyyyyyyyyyyyy? –

+0

_ "他のファイルの内容を読み取っていません。何が間違っていますか?_良い問題の説明ではありません。デバッガでコードをステップ実行しましたか? –

+0

これは、最も基本的で些細なデバッグでは捕らえられていた非常に基本的なエラーであるため、「デバッグ」が何であったかを知りたいです。 –

答えて

2

必要がある.....それで何が間違っていますデバッガの使い方最初の反復の後

path << "D:\\Text0"<< i <<".txt"; 

pathのバッファは、 "D:\ Text01.txt" が含まれています。第二の後

、それは「:\ Text01.txtD:D \ Text02.txt」が含まれてい... pathとして

が流れている、あなたがあなたをクリアし、同じ方法でそれをクリアする必要があります他のストリーム。

バッファの値を調べた場合、またはファイルを開くことができないときにテキストを出力した場合や、実際に何らかのエラー報告を追加した場合は、これを見たことがあります。

+0

ありがとうございます。それは動作します –

+0

@Rem y:あなたの主観的な物語のスタイルの変更を元に戻しましたが、あなたの修正のおかげで。 –

0

多分それは誰かを助けるでしょう。ここでは使用しないでください(readFile.is_open())。それは、現在のファイルのベクトル値に二回

#include "stdafx.h" 
    #include<iostream> 
    #include<fstream> 
    #include<string> 
    #include<sstream> 
    #include<vector> 

    using namespace std; 

int main(){ 
    ifstream stream1; 
    string str; 
    vector<string> list; 
    stringstream ss; 
    stringstream path; 
    stringstream res; 

for(int i = 1; i < 4; i++) 
{ 
path << "file" << i << ".txt"; 
stream1.open(path.str()); 

if(stream1.is_open()) 
{ 

     getline(stream1, str); 
     cout << str << endl; 
     ss << str << " \n "; 
     list.push_back(ss.str()); 

    stream1.close(); 
} 

path.str(string()); 
ss.str(string()); 
} 

cout << " ----------------- " << endl; 

for (int i = 0; i < list.size(); i++) 
{ 
    ss << list.at(i); 
    cout << ss.str(); 
    ss.str(string()); 
} 

return 0; 

}

0

を書く他の人が指摘したように、問題がpathが各ループ後にクリアされていないということです。ので、明示的に「リセット」の代わりに今

、それ(とそれだけでなく)私たちは、より一般的かつ有用な概念コールRAIIを使用することができ、変数:

技術「資源獲得は初期化です」。基本的な考え方 はローカルオブジェクトによってリソースを表現することで、ローカルの オブジェクトのデストラクタがリソースを解放するようになっています。例えばそのように、 プログラマがリソースを解放することを忘れないことができます...(https://isocpp.org/wiki/faq/exceptions#finally

は、OPのコードは、このように変更することができます。

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

int main() { 
    std::vector<std::string> vectorOfStrings; 

    for (int i = 1; i <= 4; i++) 
    { 
     std::stringstream path; 
     path << "D:\\Text0" << i << ".txt"; 
     std::ifstream readFile(path.str()); 

     if (!readFile) 
     { 
      std::cerr << "Error: Unable to open file " << path.str() << '\n'; 
      break; 
     } 
     std::string line; 
     std::stringstream ss; 
     while(getline(readFile, line)) 
     { 
      ss << line << "\n"; 
     } 
     ss << "\n ***************************************\n"; 
     vectorOfStrings.push_back(std::move(ss.str())); 
    } 

    for (const auto &s : vectorOfStrings) { 
     std::cout << s; 
    } 

    return 0; 
} 
+0

ありがとうございました。 –

関連する問題