如何替换文本文件中的行? C++

How to replace lines in a text file? C++

使用替换行方法时遇到 std::out_of_range 异常,我不确定如何解决这个问题。我知道 pos > length() 时会发生异常,但我不确定如何在不发生这种情况的情况下修改我的文本文件。我正在尝试将文本文件中的 delLine 替换为 replaceLine。感谢任何帮助,谢谢!

unsigned int start;
start = 0;
unsigned int len;
unsigned int end;
std::string line;
std::ifstream fin;
fin.open("test.txt");
std::ofstream temp;
temp.open("temp.txt");
std::string delLine = "   color = vec4";
std::string replaceString = "   color = vec4("+red+", "+ green + ", " + blue +",1.0)";

while (getline(fin, line))
{
    start = start + line.length();
    len = replaceString.length();
    end = len + start;
    if (line.find(delLine)>start) {
        line.replace(start, end, replaceString);
    }
    temp << line << std::endl;
    line = "";
}

temp.close();
fin.close();
remove("test.txt");
rename("temp.txt", "test.txt");

getline 读取 one 行(如果可能的话)但是你管理 startend 就像 line 收到文件的全部内容,因为 startend管理你尝试访问的字符串。

其中,您真的要用 "something color = vec4("+red+", "+ green + ", " + blue +",1.0)somethingelse" 替换 "something color = vec4somethingelse" 吗?可能不是,所以不要检查 line.find(delLine)(line == delLine).

另请注意,在行尾添加 "("+red+", "+ green + ", " + blue +",1.0)" 就足以获得预期结果,因为行首没有变化。

所以

std::string line;
std::ifstream fin("test.txt");
std::ofstream temp("temp.txt");

if (!fin)) {
  ...manage error...
}
else if (!temp) {
  ...manage error...
}
else {
  std::string lineToModify = "   color = vec4";
  std::string toBeAdded = "("+red+", "+ green + ", " + blue +",1.0)";

  while (getline(fin, line))
  {
    if (line == lineToModify)
      line += toBeAdded;
    temp << line << std::endl;
  }

  temp.close();
  fin.close();
  remove("test.txt"); /* you need to check success */
  rename("temp.txt", "test.txt"); /* you need to check success */
}

示例:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::string red = "RED";
  std::string green = "GREEN";
  std::string blue = "BLUE";
  
  std::string line;
  std::ifstream fin("test.txt");
  std::ofstream temp("temp.txt");
  
  if (!fin) {
    std::cerr << "cannot open test.txt" << std::endl;
    return -1;
  }
  
  if (!temp) {
    std::cerr << "cannot open temp.txt" << std::endl;
    return -1;
  }

  std::string lineToModify = "   color = vec4";
  std::string toBeAdded = "("+red+", "+ green + ", " + blue +",1.0)";
  
  while (getline(fin, line)) {
    if (line == lineToModify)
      line += toBeAdded;
    temp << line << std::endl;
  }
  
  temp.close();
  fin.close();
  
  if (remove("test.txt") != 0) {
    perror("cannot delete test.txt, result in temp.txt");
    return -1;
  }
  
  if (rename("temp.txt", "test.txt") != 0) {
    perror("cannot rename temp.txt to test.txt, result in temp.txt");
    return -1;
  }
  
  return 0;
}

编译与执行:

pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ cat test.txt 
something
   color = vec4
   color = vec44
    color = vec4
   color = vec4
end
pi@raspberrypi:/tmp $ ./a.out
pi@raspberrypi:/tmp $ cat test.txt 
something
   color = vec4(RED, GREEN, BLUE,1.0)
   color = vec44
    color = vec4
   color = vec4(RED, GREEN, BLUE,1.0)
end
pi@raspberrypi:/tmp $ 

我假设搜索行可以出现多次,否则如果可以改进代码。

如果输入文件中不存在搜索行,最好使用布尔值来了解是否至少进行了更改,如果没有,则不 delete/rename 但删除临时文件更改输入文件的 modification/creation 日期