资源简介
因工作需要,本人用c++ 编写替换文本文件中指定字符串的小程序,供大家使用:
语法
rep2 test.txt srcstr,desstr
srcstr=是将要被替换的字符串
desstr=是替换成的字符串
速度很快,但要求文件不可大于5000行。
代码片段和文件信息
#include “iostream“
#include “string“
#include “fstream“
#include “stdio.h“
using namespace std;
int main(int argcchar *argv[])
{
/*
本程序用于替换文本文件中““组成的字符串
*/
string NowTime=“0“NowTime2;
//for (int i=0;i //cout< string Newfile [5000];
if (argc>=4)
{
NowTime2=argv[2];
NowTime=argv[3];
}
else
{
cout<<“请输入参数[文件名][原字符串][替换字符串]“< exit(EXIT_FAILURE);
}
//读取文件
int i =0poscountlen;
len=NowTime2.length();
ifstream inFile;
inFile.open(argv[1]);
if (!inFile.is_open())
{
cout<<“文件打开失败!\n“;
exit(EXIT_FAILURE);
}
for(string str;getline(inFilestr);i++)
{
pos=str.find(NowTime2);
while(pos>=0)
{
str.replace(poslenNowTime);
pos=str.find(NowTime2);
}
//cout< if (i>4999 )
{
cout<<“文件内容过长,本程序不支持。“< exit(EXIT_FAILURE);
}
else
Newfile[i]=str;
}
count=i;
inFile.close();
//写入文件
ofstream outFile;
outFile.open(argv[1]);
if (!outFile.is_open())
{
cout< exit(EXIT_FAILURE);
}
for( i=0;i<=count;i++)
{
outFile< //if (Newfile[i].length()==0)
//break;
}
outFile.close();
cout< return 0;
}
//feng.zhang@uniportal.cn
//QQ:58006167
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 12800 2009-09-07 17:17 rep2.exe
文件 1304 2009-09-07 17:29 rep2.cpp
----------- --------- ---------- ----- ----
14104 2
- 上一篇:酒店房间登记与计费管理系统
- 下一篇:C++编写一个时间类Time实验报告
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- c语言字符串分割截取子串查找插入删
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
川公网安备 51152502000135号
评论
共有 条评论