【问题标题】:Alternating between reading and writing repeatedly反复读和写交替
【发布时间】:2016-03-20 02:36:59
【问题描述】:

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

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 循环?

【问题讨论】:

  • 要解决这个问题需要的字符比我现在愿意写的要多。你的方法是完全错误的。 fs.close() 显然是不正确的,但有一个更根本的问题是你不能像这样就地“替换”行。而且我不知道你在哪里听说fs.clear()“切换到写入模式”;它肯定不会。
  • 在 while 循环内关闭将阻止进一步读取。用冲洗替换关闭。同样在块的末尾继续是多余的,将其注释掉,看看是否有任何不同的行为。
  • 你基本上想要grep -w number a &gt; b ; mv b a
  • 此外,如果文件不以换行符结尾,“line.length()+1”将是错误的,这将破坏最后一行之前的换行符。如果文件不是空的,但根本没有任何换行符,这将是未定义的行为。显然,这也会在使用多字节序列表示逻辑换行符的技术有缺陷的操作系统上出错——我很无聊,我想开始一场激烈的战争,我们走吧!
  • @Barry:我发布的代码用于覆盖一行。我正在尝试将其更改为覆盖多行。

标签: c++ fstream


【解决方案1】:

我遇到了同样的问题,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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多