【问题标题】:Input line by line from an input file and tokenize using strtok() and the output into an output file从输入文件中逐行输入并使用 strtok() 进行标记化,然后输出到输出文件中
【发布时间】:2011-05-18 17:45:01
【问题描述】:

我要做的是逐行输入文件并标记化并输出到输出文件中。我能够做的是输入文件中的第一行,但我的问题是我无法输入下一行以进行标记,以便可以将其保存为输出文件中的第二行,这是迄今为止我在文件中输入第一行所能做的。

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

【问题讨论】:

    标签: c++ string visual-c++ iostream tokenize


    【解决方案1】:

    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 ,,.;:'\""
                                                              "!@#$%^&*_-=+`~/\\"
                                                              "()[]{}<>";
                               strtk::parse(line,delimiters,word_list);
                            });
    
       std::cout << strtk::join(" ",word_list) << std::endl;
    
       return 0;
    }
    

    更多例子可以在Here找到

    【讨论】:

      【解决方案2】:

      当 C++ 使这变得更整洁时,您正在使用 C 运行时库。

      在 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
      

      输入:

      a,b c

      de fg,hijklmn

      输出:

      a b c de fg hijklmn

      如果您需要换行符,请在适当的位置添加 out &lt;&lt; endl;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 2019-07-03
        • 1970-01-01
        相关资源
        最近更新 更多