C++ 从以前添加的批处理文件中删除一个字 std::ofstreamreadd(startoc,ios::out | ios::app); readd
当然,总是有很多可能的解决方案,我将解释并向您展示其中的一个 为了防止数据丢失,我们不会立即(修改)原始源文件。相反,我们将首先打开一个临时文件,然后读取源文件,替换搜索到的单词,然后将新数据输出到临时文件 完成所有搜索和替换操作后,将删除原始文件,并将临时文件重命名为原始文件 对于搜索和替换功能,我在本例中使用了C++ 从以前添加的批处理文件中删除一个字 std::ofstreamreadd(startoc,ios::out | ios::app); readd,c++,C++,当然,总是有很多可能的解决方案,我将解释并向您展示其中的一个 为了防止数据丢失,我们不会立即(修改)原始源文件。相反,我们将首先打开一个临时文件,然后读取源文件,替换搜索到的单词,然后将新数据输出到临时文件 完成所有搜索和替换操作后,将删除原始文件,并将临时文件重命名为原始文件 对于搜索和替换功能,我在本例中使用了std::regex\u replace函数。这是非常灵活的,因为您可以使用正则表达式作为搜索和替换字符串。但对于初学者来说,这可能也有点困难。无论如何,它当然也将替换用户给定的普通字
std::regex\u replace
函数。这是非常灵活的,因为您可以使用正则表达式作为搜索和替换字符串。但对于初学者来说,这可能也有点困难。无论如何,它当然也将替换用户给定的普通字符串
请注意:建议的实现不会按照OP的要求搜索和替换单词。目前无法删除整行(将保留一个空行)
那么,现在是程序流程
std::ofstream readd(startLoc, ios::out | ios::app);
readd << "/Uninstall";
readd.close();
#包括
#包括
#包括
#包括
#包括
#包括
bool searchandreplacewordinfle(const std::string和filename,const std::string和searchString,const std::string和replaceString){
//获取临时文件名
#杂注警告(抑制:4996)
const std::string tempFileName=std::tmpnam(nullptr);
//文件操作的结果
bool结果{true};
//打开fstreams的新作用域。将调用析构函数并关闭文件
//在FStream之后,它将不在范围之内
{
//打开源文件名
std::ifstream源文件(文件名);
//检查是否可以打开
如果(源文件){
//打开临时文件
std::of流tempFile(tempFileName);
//检查一下,能不能打开
如果(临时文件){
//我们将逐行阅读文本文件
std::字符串文本行{};
//读取文本文件的所有行
while(std::getline(sourceFile,textLine)){
//Serach,Replace,使用regex并在temp文件中输出
tempFile打开批处理文件进行读取。将内容读入缓冲区(astd::string
)。搜索子字符串“/Uninstall”
。如果找到,将其从字符串中删除。将缓冲区写回批处理文件(即完全覆盖)。愉快编码。;-)我应该使用什么命令删除该文本。我使用的是:std::fstream readd(startOC,“r”);readd.erase(“/Uninstall”);readd.close();我应该使用什么命令来删除该文本。std::string::erase()
?获取erase没有任何成员函数的错误
#include <string>
#include <regex>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdio>
bool searchAndReplaceWordInFile(const std::string& filename, const std::string& searchString, const std::string& replaceString) {
// Get a temporary filename
#pragma warning(suppress : 4996)
const std::string tempFileName = std::tmpnam(nullptr);
// Result of file operations
bool result{ true };
// Open a new scope for the fstreams. The destructor will be called and the files will be closed
// after the fstreams will fall out of scope
{
// Open the source file name
std::ifstream sourceFile(filename);
// Check if it could be opened
if (sourceFile) {
// Open the temp file
std::ofstream tempFile(tempFileName);
// And check, if it could be opened
if (tempFile) {
// We will read the text file line by line
std::string textLine{};
// Read all lines of text file
while (std::getline(sourceFile, textLine)) {
// Serach, Replace, using regex and output in temp file
tempFile << std::regex_replace(textLine, std::regex(searchString), replaceString) << "\n";
}
}
else {
std::cerr << "*** Error Could not open temp file: " << tempFileName << "\n";
result = false;
}
}
else {
std::cerr << "*** Error Could not open source file: " << filename << "\n";
result = false;
}
}
if (result) {
// Remove original file and rename temp file to original file
std::remove(filename.c_str());
if (std::rename(tempFileName.c_str(), filename.c_str())) result = false;
}
return result;
}
int main() {
if (searchAndReplaceWordInFile("r:\\something.bat", "/Uninstall", ""))
std::cout << "Success, Done. \n";
return 0;
}