簡體   English   中英

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

[英]How to replace lines in a text file? C++

使用替換行方法時,我遇到std::out_of_range異常,我不確定如何解決此問題。 我知道當pos > length()時會發生異常,但我不確定如何修改我的文本文件而不發生這種情況。 我正在嘗試用文本文件中的replaceLine替換delLine 任何幫助表示贊賞,謝謝!

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讀取一行(如果可能),但是您對開始結束的管理就像接收文件的全部內容一樣,因為開始結束管理您嘗試從字符串中訪問。

除此之外,您真的想用"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 $ 

我認為搜索行可以出現多次,否則可以改進代碼。

如果輸入文件中不存在搜索行,最好使用 boolean 來了解是否至少進行了更改,如果不刪除/重命名但刪除臨時文件以不更改修改/創建日期輸入文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM