2011-12-03 4 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

@ माहेश यह * अगर (! Myfile) * पहली समस्या हो सकती है। (मुझे खेद है .. एसटीएल सीखने की जरूरत है।) – Beginner

+0

@RomanB: उस पंक्ति के साथ कुछ भी गलत नहीं है। – Puppy

उत्तर

29

@ फ़ेलडेव ने वास्तव में सबसे सरल रूप सूचीबद्ध किया था।

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 आपको बाहरी जबकि लूप की आवश्यकता नहीं है। इसे हटा दो! दोनों मामलों में। क्या आप भी सुनिश्चित हैं कि आपके वैक्टर में 12 और 20 लाइनें मौजूद हैं? – FailedDev

+0

हाँ, मैं सकारात्मक हूं कि मेरी टेस्ट फाइलों में 20 से अधिक भरे लाइनें हैं। और मुझे त्रुटि हो रही है कि त्रुटि खोलने वाली आउटपुट फ़ाइल – user977154

16

सरल संस्करण:

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

मैं छोड़ते हुए कर रहा हूँ एक विकल्प के रूप में, यहाँ कैसे मैं अक्सर कि पाश कोड है शामिल है और अन्य गंक। मेरा संस्करण लगभग असफल डीव के समान है लेकिन 'फॉर' लूप का उपयोग करके मैंने लूप में 'लाइन' की घोषणा की है। यह लाइन गिनती को कम करने के लिए सिर्फ एक चाल नहीं है। ऐसा करने से लाइन के दायरे कम हो जाते हैं - यह लूप के बाद गायब हो जाता है। सभी चरों में सबसे छोटा गुंजाइश होना चाहिए, इसलिए यह बेहतर है। लूप के लिए भयानक हैं।

+0

उत्कृष्ट। यह 'नेमस्पेस std का उपयोग कर' के साथ भी क्लीनर है, इसलिए सभी 'std ::' को हटाया जा सकता है। 'Ifs' घोषणा गुम है, घोषित की गई: 'ifstream ifs (textFilePath, ios :: in); ' –

संबंधित मुद्दे