【问题标题】:How to write multiple text files from a text file in C++?如何从 C++ 中的文本文件写入多个文本文件?
【发布时间】:2021-12-04 17:11:58
【问题描述】:

我有一个 txt 文件,它有 500,000 行,每行有 5 列。我想从这个文件中读取数据并将其写入不同的 5000 个 txt 文件,每个文件有 100 行,从输入文件的第一行到最后一行。此外,文件名与订单号一起输出,例如“1_Hello.txt”,它从第 1 行到第 100 行,“2_Hello.txt”,从第 101 行到第 200 行,依此类推,直到“5000_Hello.txt”。 txt",第 499901 行到第 500000 行。

我曾经运行以下代码来编写少于 10 个文件的文件。但是在 5000 个文本文件的情况下如何编写呢?任何帮助将不胜感激。

 #include <iostream>
 #include <fstream>
 #include <vector>
 using namespace std;

 int main() {

 vector<string> VecData;
 string data;

 ifstream in("mytext.txt");
 while (in >> data) {
    VecData.push_back(data);
 }
 in.close();


 ofstream mynewfile1;
 char filename[]="0_Hello.txt";

 int i, k=0, l=0;
 while(l<VecData.size()){
    for(int j='1';j<='3';j++){            
        filename[0]=j;
        mynewfile1.open(filename);
        for( i=k; i<k+((int)VecData.size()/3);i+=5){
            mynewfile1<<VecData[i]<<"\t";
            mynewfile1<<VecData[i+1]<<"\t";
            mynewfile1<<VecData[i+2]<<"\t";
            mynewfile1<<VecData[i+3]<<"\t";
            mynewfile1<<VecData[i+4]<<endl;
        }
        mynewfile1.close();
        l=i;
        k+=(int)VecData.size()/3; 
    }
 }

 cout<<"Done\n";
 return 0;
}

【问题讨论】:

    标签: c++ string readfile writefile


    【解决方案1】:

    您已经得到了答案,但我会给出一个使用 std::copy_nstd::istream_iteratorstd::ostream_iterator 的替代方案。

    这一次将 100 行复制到当前输出文件。

    我添加了一个包装 std::string 的类,以便能够为字符串提供我自己的流式操作符,使其一次读取一行。

    #include <algorithm>
    #include <fstream>
    #include <iostream>
    
    struct Line {
        std::string str;
    };
    
    std::istream& operator>>(std::istream& is, Line& l) {
        return std::getline(is, l.str);
    }
    
    std::ostream& operator<<(std::ostream& os, const Line& l) {
        return os << l.str << '\n';
    }
    
    int main() {
        if(std::ifstream in("mytext.txt"); in) {
            for(unsigned count = 1;; ++count) {
    
                // Constructing an istream_operator makes it read one value from the
                // stream and if that fails, it'll set the eofbit on the stream.
    
                std::istream_iterator<Line> in_it(in);
                if(!in) break; // EOF - nothing more in the input file
    
                // Open the output file and copy 100 lines into it:
                if(std::ofstream out(std::to_string(count) + "_Hello.txt"); out) {
                    std::copy_n(in_it, 100, std::ostream_iterator<Line>(out));
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      你工作太辛苦了——你不需要先阅读整个输入,也不需要关心每一行的结构。

      逐行读写,一次一百行。
      没有什么要读的时候停下来。

      应该这样做:

      int main()
      {
          std::ifstream in("mytext.txt");
          int index = 0;
          std::string line;
          while (in)
          {
              std::string name(std::to_string(index) + "_Hello.txt");
              std::ofstream out(name);
              for (int i = 0; i < 100 && std::getline(in, line); i++)
              {
                  out << line << '\n';
              }
              index += 1;
          }
          cout << "Done\n";
      }
      

      【讨论】:

      • 哦,我明白了。问题解决得很好。非常感谢
      • 一个细节:这会在最后创建一个空文件,不是吗?
      • 是的,它会创建
      • @carter 好的,这应该很容易解决。在打开out 之前,只需阅读in 中的一行。如果读取该行失败,则中断,否则,在打开out 后直接将其放入out,并将以下循环改为在 99 处结束。 Like this
      • 太棒了。谢谢
      猜你喜欢
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2020-11-12
      • 2021-10-12
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多