C++替换文件中指定的内容

/*实现替换文件中指定的内容
----Created by cryking----
--------2012.02.12--------*/
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;

char *strstr_rep(char *source,char *old,char *ne)//字符替换
{
 char *org=source;
 char temp[256];
 int old_length=strlen(old);//获得将被替换的字符串的大小
 int i,j,k,location=-1;
 for(i=0;source[i]&&(location==-1);++i)//location查找将被替换的字符串的位置
  for(j=i,k=0;source[j]==old[k];j++,k++)
   if(!old[k+1])
    location=i;
   if(location!=-1)//开始替换
   {
    for(j=0;j<location;j++)//先把被替换的字符串的前一部分COPY到temp
     temp[j]=source[j];
    for(i=0;ne[i];i++,j++)//再把替换的新字符串COPY到temp
     temp[j]=ne[i];
    for(k=location+old_length;source[k];k++,j++)//把剩下的内容COPY到temp
     temp[j]=source[k];
    temp[j]=NULL;
    for(i=0;source[i]=temp[i];i++); //把临时字符串temp复制给source
   }
   return org;  
}
int main()
{
 fstream outfile("test.txt",ios::out|ios::in);
 char ch;
 char buffer[255];
 int i=0,k=0;
 if(!outfile){  
  cout<<"不能打开目的文件:test.txt"<<'\n';  
  exit(1);  
 }
 
 outfile.unsetf(ios::skipws); 
 while (outfile>>ch) {//将文件全部内容读出到buffer
  buffer[i]=ch;
     i++;
 } 
 strstr_rep(buffer,"2222","8888");//将"2000"替换为8888"
 outfile.close();
  ofstream infile("test.txt");
  while(k!=i){infile<<buffer[k];k++;}//将buffer全部写入到文件
  outfile.close();
  return 0;
}
很久没敲代码了,唉。。。手生了!!!
### 实现C++替换文件指定位置的字符 要在C++中实现替换文件指定位置的字符,通常需要以下几个步骤: 1. 打开文件以读写模式访问。 2. 移动到目标位置。 3. 替换该位置上的字符。 4. 关闭文件。 以下是具体的代码示例以及解释: ```cpp #include <iostream> #include <fstream> void replaceCharacterInFile(const std::string& filename, size_t position, char newChar) { // 使用 fstream 类打开文件,设置 ios::in 和 ios::out 模式以便于修改文件内容 std::ifstream file(filename, std::ios::in | std::ios::binary); if (!file.is_open()) { std::cerr << "Error opening the file." << std::endl; return; } // 获取文件大小 file.seekg(0, std::ios::end); size_t fileSize = static_cast<size_t>(file.tellg()); file.seekg(0, std::ios::beg); // 如果指定位置超出文件范围,则报错退出 if (position >= fileSize) { std::cerr << "Position out of range." << std::endl; file.close(); return; } // 创建临时缓冲区保存整个文件内容 std::vector<char> buffer(fileSize); file.read(buffer.data(), fileSize); file.close(); // 修改指定位置的字符 buffer[position] = newChar; // 将修改后的内容重新写入文件 std::ofstream outFile(filename, std::ios::out | std::ios::trunc | std::ios::binary); if (!outFile.is_open()) { std::cerr << "Error reopening the file for writing." << std::endl; return; } outFile.write(buffer.data(), fileSize); outFile.close(); } int main() { std::string fileName = "example.txt"; size_t pos = 5; // 假设我们要替换第6个字符(索引从0开始) char replacement = 'X'; replaceCharacterInFile(fileName, pos, replacement); std::cout << "Character replaced successfully at position " << pos << "." << std::endl; return 0; } ``` #### 解释 - 文件通过 `std::ifstream` 以二进制模式 (`std::ios::binary`) 打开,这样可以精确控制字节级别的数据操作[^1]。 - 需要先获取文件大小,并判断给定的位置是否超出了文件的实际长度。如果超出,则抛出错误提示[^2]。 - 整个文件被加载到内存中的动态数组 `buffer` 中,便于随机存取任意位置的数据[^3]。 - 对应位置的字符被替换成新的字符之后,再将更新后的缓冲区写回原文件[^4]。 这种方法虽然简单易懂,但对于非常大的文件可能不太高效,因为它会一次性把整个文件载入内存。对于更大的文件,建议采用分块读写的策略来优化性能。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值