2010-12-01 16 views
13

जो मैं करने की कोशिश कर रहा हूं वह है कि लाइन लाइन द्वारा लाइन इनपुट करें और आउटपुट फ़ाइल में टोकन और आउटपुट करें। मेरे पास क्या है करने में सक्षम है फ़ाइल में पहली पंक्ति इनपुट है लेकिन मेरी समस्या यह है कि मैं अगली पंक्ति को टोकननाइज़ करने के लिए इनपुट करने में असमर्थ हूं ताकि इसे आउटपुट फ़ाइल में दूसरी पंक्ति के रूप में सहेजा जा सके, यह वही है जो मैं अब तक कर सकता था फ़ाइल में पहली पंक्ति इनपुट करने के लिए।इनपुट फ़ाइल से लाइन द्वारा इनपुट लाइन और strtok() और आउटपुट फ़ाइल में उत्पादन आउटपुट फ़ाइल

#include <iostream> 
#include<string> //string library 
#include<fstream> //I/O stream input and output library 

using namespace std; 
const int MAX=300; //intialization a constant called MAX for line length 
int main() 
{ 
    ifstream in;  //delcraing instream 
    ofstream out; //declaring outstream 

    char oneline[MAX]; //declaring character called oneline with a length MAX 

    in.open("infile.txt"); //open instream 
    out.open("outfile.txt"); //opens outstream 
    while(in) 
    { 

    in.getline(oneline,MAX); //get first line in instream 

    char *ptr;  //Declaring a character pointer 
    ptr = strtok(oneline," ,"); 
    //pointer scans first token in line and removes any delimiters 


    while(ptr!=NULL) 
    { 

    out<<ptr<<" "; //outputs file into copy file 
    ptr=strtok(NULL," ,"); 
    //pointer moves to second token after first scan is over 
    } 

    } 
    in.close();  //closes in file 
    out.close();  //closes out file 


    return 0; 
} 

उत्तर

1

सी ++ इस नीदर को बनाता है जब आप सी रनटाइम लाइब्रेरी का उपयोग कर रहे हैं।

कोड C++ यह करने के लिए:

ifstream in;  //delcraing instream 
ofstream out; //declaring outstream 

string oneline; 

in.open("infile.txt"); //open instream 
out.open("outfile.txt"); //opens outstream 
while(getline(in, oneline)) 
{ 
    size_t begin(0); 
    size_t end; 
    do 
    { 
     end = oneline.find_first_of(" ,", begin); 

     //outputs file into copy file 
     out << oneline.substr(begin, 
        (end == string::npos ? oneline.size() : end) - begin) << ' '; 

     begin = end+1; 
     //pointer moves to second token after first scan is over 
    } 
    while (end != string::npos); 
} 

in.close();  //closes in file 
out.close();  //closes out file 

इनपुट:

क, ख ग

डी FG, hijklmn

आउटपुट:

a b c डी FG hijklmn

आप नई-पंक्तियों चाहते हैं, उचित बिंदु पर out << endl; जोड़ें।

14

C++ String Toolkit Library (StrTk) आपकी समस्या के लिए निम्न समाधान है:

#include <iostream> 
#include <string> 
#include <deque> 
#include "strtk.hpp" 

int main() 
{ 
    std::deque<std::string> word_list; 
    strtk::for_each_line("data.txt", 
         [&word_list](const std::string& line) 
         { 
          const std::string delimiters = "\t\r\n ,,.;:'\"" 
                  "[email protected]#$%^&*_-=+`~/\\" 
                  "()[]{}<>"; 
          strtk::parse(line,delimiters,word_list); 
         }); 

    std::cout << strtk::join(" ",word_list) << std::endl; 

    return 0; 
} 

अधिक उदाहरण पाया जा सकता है Here