c++ - 反复读写交替

标签 c++ fstream

我的目标是逐行读取文件,检查该行是否包含一些数字,如果是则重写该行。然后继续读取文件。 我已经成功地对一行执行此操作,但我不知道如何继续阅读文件的其余部分。 下面是我如何替换一行(每一行都是已知的固定大小):

while(getline(fs, line)){
  if(condition){
    pos = fs.tellg();       //gets current read position (end of the line I want to change)
    pos -= line.length()+1;    //position of the beginning of the line
    fs.clear();             //switch to write mode
    fs.seekp(pos);          //seek to beginning of line
    fs << new_data;         //overwrite old data with new data (also fixed size)
    fs.close();             //Done.
    continue;
  }
}

如何切换回读取并继续 getline 循环?

最佳答案

我有同样的问题,TB级文件,我想修改文件开头的一些头信息。 显然,当一个人最初为任何新内容创建文件时必须留出足够的空间,因为没有办法增加文件大小(除了附加到它之外)并且新行必须具有与原始行完全相同的行长.

这里是我的代码的简化:

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

bool CreateDummy()
{
  ofstream out;
  out.open("Dummy.txt");
  // skip: test if open

  out<<"Some Header"<<endl;
  out<<"REPLACE1  12345678901234567890"<<endl;
  out<<"REPLACE2  12345678901234567890"<<endl;
  out<<"Now ~1 TB of data follows..."<<endl;

  out.close();

  return true;
}


int main()
{
  CreateDummy(); // skip: test if successful

  fstream inout;
  inout.open("Dummy.txt", ios::in | ios::out);
  // skip test if open

  bool FoundFirst = false;
  string FirstText = "REPLACE1";
  string FirstReplacement = "Replaced first!!!";

  bool FoundSecond = false;
  string SecondText = "REPLACE2";
  string SecondReplacement = "Replaced second!!!";

  string Line;
  size_t LastPos = inout.tellg();
  while (getline(inout, Line)) {
    if (FoundFirst == false && Line.compare(0, FirstText.size(), FirstText) == 0) {
      // skip: check if Line.size() >= FirstReplacement.size()
      while (FirstReplacement.size() < Line.size()) FirstReplacement += " ";
      FirstReplacement += '\n';

      inout.seekp(LastPos);
      inout.write(FirstReplacement.c_str(), FirstReplacement.size());
      FoundFirst = true;
    } else if (FoundSecond == false && Line.compare(0, SecondText.size(), SecondText) == 0) {
      // skip: check if Line.size() >= SecondReplacement.size()
      while (SecondReplacement.size() < Line.size()) SecondReplacement += " ";
      SecondReplacement += '\n';

      inout.seekp(LastPos);
      inout.write(SecondReplacement.c_str(), SecondReplacement.size());
      FoundSecond = true;
    }

    if (FoundFirst == true && FoundSecond == true) break;
    LastPos = inout.tellg();
  }
  inout.close();

  return 0;
}

输入是

Some Header
REPLACE1  12345678901234567890             
REPLACE2  12345678901234567890             
Now ~1 TB of data follows...

输出是:

Some Header
Replaced first!!!             
Replaced second!!!            
Now ~1 TB of data follows...

关于c++ - 反复读写交替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36109653/

相关文章:

java - 社交网络架构、C++ Clients Neo4j 数据库之间有什么关系?

c++ - C++ boost 绑定(bind)性能

c++ - 调用由指针非静态初始化的类方法

c++ - 尝试从 istream 存储 token 时出现段错误

C++ 基本文件 i/o,读取失败

C++ 与 sqlite : "library routine called out of sequence

C++:file.seekg() 似乎没有返回当前位置

c++ - 从 'int' 到 'char *' 的无效转换

c++ - 在 C++ 中将变量转换和写入文件的最佳方式(性能驱动)是什么?

c++ - 将数据从 cv::Mat 复制到 CvMat,反之亦然