2010-12-01 3 views
13

Ce que j'essaie de faire est d'entrer un fichier LINE BY LINE et tokenize et de sortie dans un fichier de sortie. été en mesure de faire est d'entrer la première ligne dans le fichier, mais mon problème est que je suis incapable d'entrer la ligne suivante tokenize afin qu'il puisse être enregistré comme une deuxième ligne dans le fichier de sortie, c'est ce que je pouvais faire jusqu'à présent De l'entrée de la première ligne dans le fichier.Entrée ligne par ligne à partir d'un fichier d'entrée et tokenize en utilisant strtok() et la sortie dans un fichier de sortie

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

Répondre

1

Vous utilisez la bibliothèque d'exécution C lorsque C++ rend ce système plus pratique.

Code pour le faire en 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 

Entrée:

a, b c

de fg, hijklmn

Sortie:

a b c de fg hijklmn

Si vous voulez des nouvelles lignes, ajoutez out << endl; au moment approprié.

14

Le C++ String Toolkit Library (StrTk) a la solution suivante à votre problème:

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

Plus exemples peuvent être trouvés Here

Questions connexes