2011-12-03 71 views
20

我想讀取每行包含一個單詞的文本文件的每一行,並將這些單詞放入一個向量中。我會怎麼做呢?從文本文件讀取行並將字符串放入向量中?

這是我的新代碼:我認爲它仍然有問題。

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

int main() 
{ 
    std::string line; 
    vector<string> DataArray; 
    vector<string> QueryArray; 
    ifstream myfile("OHenry.txt"); 
    ifstream qfile("queries.txt"); 

    if(!myfile) //Always test the file open. 
    { 
     cout<<"Error opening output file"<<endl; 
     system("pause"); 
     return -1; 
    } 
    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 
    if(!qfile) //Always test the file open. 
    { 
     cout<<"Error opening output file"<<endl; 
     system("pause"); 
     return -1; 
    } 

    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 

    cout<<QueryArray[0]<<endl; 
    cout<<DataArray[0]<<endl; 

} 
+2

你到目前爲止的代碼有什麼問題? – Mahesh

+0

@Mahesh this * if(!myfile)*可能是第一個問題。 (我很抱歉...需要學習STL。) – Beginner

+0

@RomanB:這條線沒什麼問題。 – Puppy

回答

29

@FailedDev的確的確列出了最簡單的形式。作爲替代,這裏是如何我經常代碼迴路:

std::vector<std::string> myLines; 
std::copy(std::istream_iterator<std::string>(myfile), 
      std::istream_iterator<std::string>(), 
      std::back_inserter(myLines)); 

整個程序可能是這樣的:

// Avoid "using namespace std;" at all costs. Prefer typing out "std::" 
// in front of each identifier, but "using std::NAME" isn't (very) dangerous. 
#include <iostream> 
using std::cout; 
using std::cin; 
#include <fstream> 
using std::ifstream; 
#include <string> 
using std::string; 
#include <vector> 
using std::vector; 
#include <iterator> 
using std::istream_iterator; 
#include <algorithm> 
using std::copy; 

int main() 
{ 

    // Store the words from the two files into these two vectors 
    vector<string> DataArray; 
    vector<string> QueryArray; 

    // Create two input streams, opening the named files in the process. 
    // You only need to check for failure if you want to distinguish 
    // between "no file" and "empty file". In this example, the two 
    // situations are equivalent. 
    ifstream myfile("OHenry.txt"); 
    ifstream qfile("queries.txt"); 

    // std::copy(InputIt first, InputIt last, OutputIt out) copies all 
    // of the data in the range [first, last) to the output iterator "out" 
    // istream_iterator() is an input iterator that reads items from the 
    // named file stream 
    // back_inserter() returns an interator that performs "push_back" 
    // on the named vector. 
    copy(istream_iterator<string>(myfile), 
     istream_iterator<string>(), 
     back_inserter(DataArray)); 
    copy(istream_iterator<string>(qfile), 
     istream_iterator<string>(), 
     back_inserter(QueryArray)); 

    try { 
     // use ".at()" and catch the resulting exception if there is any 
     // chance that the index is bogus. Since we are reading external files, 
     // there is every chance that the index is bogus. 
     cout<<QueryArray.at(20)<<"\n"; 
     cout<<DataArray.at(12)<<"\n"; 
    } catch(...) { 
     // deal with error here. Maybe: 
     // the input file doesn't exist 
     // the ifstream creation failed for some other reason 
     // the string reads didn't work 
     cout << "Data Unavailable\n"; 
    } 
} 
+0

我需要什麼包含和命名空間? – user977154

+1

甜得到它的工作。非常感謝。這確實是非常簡單和清潔。 – user977154

+0

@ user977154看到上面的完整示例 –

28

最簡單的形式:

std::string line; 
std::vector<std::string> myLines; 
while (std::getline(myfile, line)) 
{ 
    myLines.push_back(line); 
} 

沒有必要瘋狂ç一樣的東西:)

編輯:

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

int main() 

{ 
    std::string line; 
    std::vector<std::string> DataArray; 
    std::vector<std::string> QueryArray; 
    std::ifstream myfile("OHenry.txt"); 
    std::ifstream qfile("queries.txt"); 

    if(!myfile) //Always test the file open. 
    { 
     std::cout<<"Error opening output file"<< std::endl; 
     system("pause"); 
     return -1; 
    } 
    while (std::getline(myfile, line)) 
    { 
     DataArray.push_back(line); 
    } 

    if(!qfile) //Always test the file open. 
    { 
     std::cout<<"Error opening output file"<<std::endl; 
     system("pause"); 
     return -1; 
    } 

    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 

    std::cout<<QueryArray[20]<<std::endl; 
    std::cout<<DataArray[12]<<std::endl; 
    return 0; 
} 

關鍵字使用是非法的C++!切勿使用它。好?好。現在比較我寫的和你寫的內容,並試圖找出差異。如果你仍然有問題回來。

+0

我在我的帖子中修復了代碼,現在我做錯了什麼?因爲我需要使用兩個不同的文本文件。順便謝謝你的幫助。 – user977154

+0

@ user977154您不需要外部while循環。去掉它!在這兩種情況下。你也確定你的向量中有12行和20行嗎? – FailedDev

+0

是的,我的積極性是我的測試文件中有20多條填充行。我不斷收到錯誤說錯誤打開輸出文件 – user977154

16

最簡單的版本:

std::vector<std::string> lines; 
for (std::string line; std::getline(ifs, line); /**/) 
    lines.push_back(line); 

我省略了包括和其他gunk。我的版本與FailedDev幾乎相同,但是通過使用'for'循環,我在循環中放入了'line'聲明。這不僅僅是減少線數的一個技巧。這樣做會減少行的範圍 - 它在for循環之後會消失。所有變量應該有可能的最小範圍,所以這樣做更好。 For循環很棒。

+0

非常好。它甚至比'使用命名空間標準;'更清潔,因此所有的'std ::'都可以被刪除。 'ifs'聲明丟失,聲明如下:'ifstream ifs(textFilePath,ios :: in);' –

相關問題