Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 反复读写交替_C++_Fstream - Fatal编程技术网

C++ 反复读写交替

C++ 反复读写交替,c++,fstream,C++,Fstream,我的目标是逐行读取文件,检查该行是否包含一些数字,如果包含,则重写该行。然后继续读取该文件。 我已经成功地完成了一行,但我不知道如何继续读取文件的其余部分。 下面是我如何替换一行(每行都是已知的固定大小): while(getline(fs,line)){ 如果(条件){ pos=fs.tellg();//获取当前读取位置(我要更改的行的末尾) pos-=line.length()+1;//行首的位置 fs.clear();//切换到写入模式 fs.seekp(pos);//寻找行首 fs我也遇

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

while(getline(fs,line)){
如果(条件){
pos=fs.tellg();//获取当前读取位置(我要更改的行的末尾)
pos-=line.length()+1;//行首的位置
fs.clear();//切换到写入模式
fs.seekp(pos);//寻找行首

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

以下是我的代码的简化:

#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
Replaced first!!!             
Replaced second!!!            
Now ~1 TB of data follows...

解决这个问题需要的字符比我现在愿意写的要多。你的方法完全错误。
fs.close()
显然是不正确的,但有一个更根本的问题,你不能像这样“替换”行。我不知道你从哪里听说
fs.clear()
“切换到写入模式”;当然不会。在while循环内关闭将阻止进一步读取。将close替换为flush。在块末尾继续也是多余的,请将其注释掉,看看行为是否有任何差异。如果文件没有以换行符结尾,则基本上需要
grep-w number a>b;mv b a
呃,“line.length()+1”将是错误的,这将在最后一行之前删除换行符。如果文件为非空,但根本没有任何换行符,这将是未定义的行为。显然,这也会在技术上有缺陷的操作系统上出错,这些操作系统使用多字节序列表示逻辑换行符--我很无聊,我想发动一场火焰之战,走吧!@Barry:我发布的代码是用来覆盖一行的。我正试图将其更改为覆盖多行。
Some Header
REPLACE1  12345678901234567890             
REPLACE2  12345678901234567890             
Now ~1 TB of data follows...
Some Header
Replaced first!!!             
Replaced second!!!            
Now ~1 TB of data follows...