c++ - Input line by line from an input file and tokenize using strtok() and the output into an output file -
what trying input file line line , tokenize , output output file.what have been able input first line in file problem unable input next line tokenize saved second line in output file,this far fro inputing first line in file.
#include <iostream> #include<string> //string library #include<fstream> //i/o stream input , output library using namespace std; const int max=300; //intialization constant called max line length int main() { ifstream in; //delcraing instream ofstream out; //declaring outstream char oneline[max]; //declaring character called oneline 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 character pointer ptr = strtok(oneline," ,"); //pointer scans first token in line , removes delimiters while(ptr!=null) { out<<ptr<<" "; //outputs file copy file ptr=strtok(null," ,"); //pointer moves second token after first scan on } } in.close(); //closes in file out.close(); //closes out file return 0; }
the c++ string toolkit library (strtk) has following solution problem:
#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 ,,.;:'\"" "!@#$%^&*_-=+`~/\\" "()[]{}<>"; strtk::parse(line,delimiters,word_list); }); std::cout << strtk::join(" ",word_list) << std::endl; return 0; }
more examples can found here
Comments
Post a Comment