C++ 将一个文件里面的相同的一个字符串替换成另一个字符串
2014-04-11 11:09
357 查看
C++ 将一个文件里面的相同的一个字符串替换成另一个字符串
// change_str_in_file.cpp : Defines the entry point for the console application.
//
/*
How to change one string in a file ?
use this application.
Created 2014-4-11 by DMD
*/
/*********************************************
Example:
change_str.bat
@echo on
replace1to2 "a.txt" "HELLO" "123"
pause
@echo off
********************************************/
#include "stdafx.h"
#include <iostream>
using namespace std;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <wx\wx.h>
#include <string>
// wxWidget Library
#include <wx/ffile.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <fstream>
bool replace_string(const wxString& filename,
const wxString& strOld,
const wxString& strNew);
int main(int argc,char *argv[])//设定参数
{
cout<<"Hello! start change string:"<<endl;
printf("input 1: %s\n",argv[1]);
printf("input 2: %s\n",argv[2]);
replace_string(argv[1],argv[2],argv[3]);
Sleep(100);
return 0;
}
/****************************************************
replace strOld to strNew in a file
*****************************************************/
bool replace_string(const wxString& filename,
const wxString& strOld,
const wxString& strNew)
{
wxFFile filehandler;
wxString FileName = filename;
wxString Content = "";
if(wxFileExists(FileName))
{
printf("Successfully!, Open file: %s .\n",filename.c_str() );
filehandler.Open(FileName);
filehandler.ReadAll(&Content);
filehandler.Close();
}
else
{
printf("File: %s is not exist ! \n",filename.c_str());
return false;
}
int ifind =Content.find(strOld);
if(ifind>=0)
{
int iCount = Content.Replace(strOld,strNew);
if(iCount > 0)
{
wxFile file1(FileName, wxFile::write);
if (!file1.IsOpened())
{
file1.Close();
return false;
}
file1.Write(Content);
file1.Close();
}
}
return true;
}
完!
测试成功!
// change_str_in_file.cpp : Defines the entry point for the console application.
//
/*
How to change one string in a file ?
use this application.
Created 2014-4-11 by DMD
*/
/*********************************************
Example:
change_str.bat
@echo on
replace1to2 "a.txt" "HELLO" "123"
pause
@echo off
********************************************/
#include "stdafx.h"
#include <iostream>
using namespace std;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <wx\wx.h>
#include <string>
// wxWidget Library
#include <wx/ffile.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <fstream>
bool replace_string(const wxString& filename,
const wxString& strOld,
const wxString& strNew);
int main(int argc,char *argv[])//设定参数
{
cout<<"Hello! start change string:"<<endl;
printf("input 1: %s\n",argv[1]);
printf("input 2: %s\n",argv[2]);
replace_string(argv[1],argv[2],argv[3]);
Sleep(100);
return 0;
}
/****************************************************
replace strOld to strNew in a file
*****************************************************/
bool replace_string(const wxString& filename,
const wxString& strOld,
const wxString& strNew)
{
wxFFile filehandler;
wxString FileName = filename;
wxString Content = "";
if(wxFileExists(FileName))
{
printf("Successfully!, Open file: %s .\n",filename.c_str() );
filehandler.Open(FileName);
filehandler.ReadAll(&Content);
filehandler.Close();
}
else
{
printf("File: %s is not exist ! \n",filename.c_str());
return false;
}
int ifind =Content.find(strOld);
if(ifind>=0)
{
int iCount = Content.Replace(strOld,strNew);
if(iCount > 0)
{
wxFile file1(FileName, wxFile::write);
if (!file1.IsOpened())
{
file1.Close();
return false;
}
file1.Write(Content);
file1.Close();
}
}
return true;
}
完!
测试成功!
相关文章推荐
- php关于一个文件夹所有的文件复制到另一个文件夹里面去,相同的文件名覆盖
- 使用wxString实现字符串在一个文件里面的替换
- [C++] 在文件里面找字符串,然后在前面增加一个字符串
- 将一个目录下所有指定文件里面的特定字符串替换
- C++实现将一个字符串中的字符替换成另一个字符串的方法
- 将一个目录下所有指定文件里面的特定字符串替换
- poj 3630 Phone List 【字典树】【判断一个字符串集里面 是否存在一个字符串是另一个字符串前缀】
- C++之String的find方法,查找一个字符串在另一个字符串的什么位置;leveldb字符串转数字解析办法
- 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
- 找出自己应用程序里面所有的中文字符串,替换为字符串变量,并将所有字符串放到一个文本文件中。
- 一个替换某文件夹下的所有文件中特定字符串的小工具
- Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例
- [C++] 批量删除文件名多余的一个相同字符串
- C++中不同指针指向同一个字符串,然而地址相同
- C++_USACO_求一个字符串的连续相同字符的最大长度
- 建立一个程序,写2个线程,同时对一个文件里面写入字符串,另外一个程序读取输出在控制台,保证读取的实时性。
- 1、读取一个文件,并替换其中指定的字符串为特定字符串
- 一个进程对一个文件加写锁后,另一个进程对相同的文件仍可以以读的模式打开,但是再次加写锁失败。
- 使用C++ 复制一个文件夹下的所有文件到另一个文件夹
- 查询一个文件里面的内容是否出现在另一个文件里