// 将文件中的某个字符全部替换成其他的
void replaceBackslashWithForwardslash(const std::string& filename)
{
std::ifstream inFile(filename); // 以读取模式打开文件
if (!inFile.is_open())
{
std::cout << "Failed to open file: " << filename << std::endl;
return;
}
std::string content;
std::string line;
while (std::getline(inFile, line))
{
// 替换每行中的 \ 为 /
size_t pos = 0;
while ((pos = line.find('\\', pos)) != std::string::npos)
{
line.replace(pos, 1, "/");
pos += 1; // 跳过当前替换后的斜杠字符
}
content += line + "\n";
}
inFile.close();
std::ofstream outFile(filename); // 重新以写入模式打开同名文件
if (!outFile.is_open()) {
std::cout << "Failed to open file for writing: " << filename << std::endl;
return;
}
outFile << content; // 将替换后的内容写回文件
outFile.close();
std::cout << "File '" << filename << "' processed successfully." << std::endl;
}
替换文件中的某个指定字符
最新推荐文章于 2024-08-12 16:55:46 发布