2011-12-03 36 views
20

Tôi đang cố gắng đọc từng dòng của một tệp văn bản mà mỗi dòng chứa một từ và đặt các từ đó thành một vectơ. Làm thế nào tôi sẽ đi về làm điều đó?Đọc dòng từ tệp văn bản và đặt các chuỗi vào một vectơ?

Đây là mã mới của tôi: Tôi nghĩ rằng vẫn còn điều gì đó sai trái với nó.

#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

Bạn có vấn đề gì với mã cho đến nay? – Mahesh

+0

@Mahesh this * if (! Myfile) * có thể là vấn đề đầu tiên. (Tôi xin lỗi .. cần phải học STL.) – Beginner

+0

@RomanB: Không có gì sai với dòng đó. – Puppy

Trả lời

29

@ FailedDev thực sự đã liệt kê biểu mẫu đơn giản nhất. Là một thay thế, đây là cách tôi thường mã hóa vòng lặp rằng:

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

Toàn bộ chương trình có thể trông như thế này:

// 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

những gì bao gồm và không gian tên nào tôi cần? – user977154

+1

ngọt ngào khiến nó hoạt động. Cảm ơn bạn rất nhiều. Điều này chắc chắn là sooo dễ dàng hơn và sạch hơn. – user977154

+0

@ user977154 xem ví dụ đầy đủ ở trên –

28

hình thức đơn giản nhất:

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

Không cần thingies c điên :)

Edit:

#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; 
} 

từ khóa sử dụng là C++ bất hợp pháp! Không bao giờ sử dụng nó. ĐƯỢC? Tốt. Bây giờ so sánh những gì tôi đã viết với những gì bạn đã viết và cố gắng tìm ra sự khác biệt. Nếu bạn vẫn còn câu hỏi trở lại.

+0

tôi đã sửa mã trong bài đăng của mình, tôi đang làm gì sai? vì tôi cần làm việc với hai tệp văn bản khác nhau. Cảm ơn bạn rất nhiều vì đã giúp đỡ. – user977154

+0

@ user977154 Bạn không cần vòng lặp trong khi bên ngoài. Gỡ bỏ nó! Trong cả hai trường hợp. Bạn cũng chắc chắn có tồn tại 12 và 20 dòng trong vectơ của bạn? – FailedDev

+0

Có i am tích cực có hơn 20 dòng đầy trong các tập tin thử nghiệm của tôi. Và tôi tiếp tục nhận được lỗi nói lỗi mở tập tin đầu ra – user977154

16

đơn giản phiên bản:

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

Tôi bỏ qua bao gồm và gunk khác. Phiên bản của tôi gần như giống như của FailedDev nhưng bằng cách sử dụng một vòng lặp for 'tôi đặt tuyên bố' dòng 'trong vòng lặp. Đây không chỉ là một mẹo để giảm số lượng dòng. Làm điều này làm giảm phạm vi của dòng - nó biến mất sau vòng lặp for. Tất cả các biến nên có phạm vi nhỏ nhất có thể, do đó, điều này tốt hơn. Đối với vòng là tuyệt vời.

+0

Tuyệt vời. Nó thậm chí còn sạch hơn với 'sử dụng namespace std;' để tất cả 'std ::' có thể được loại bỏ. Khai báo 'ifs' bị thiếu, được khai báo như: 'ifstream ifs (textFilePath, ios :: in);' –

Các vấn đề liên quan