#include "stdafx.h" #include <iostream> #include <string> #include "common.h" using namespace std; int main(int argc,char *argv[]) { // 目标文件名 string szDestFileName; // 源文件名 string szSrcFileName; //vs2008中使用 设定标准库 解决中文路径 locale::global(std::locale("")); szSrcFileName = string(argv[0]);//replace_all(tmp, "//", ""); //生成目标文件名 szDestFileName = getDestFileName(szSrcFileName); //复制文件 createDestFile(szSrcFileName, szDestFileName); return 0; } 截取原来的文件名,去掉.exe的后缀,新文件命名为 xx_new.exe string getDestFileName(string szFileName) { string szNewFileName; //replace_all(szFileName, "//", ""); int nFileNameLen = szFileName.length(); char *pFilePath, *pFileName = new char[nFileNameLen + 1]; strcpy_s(pFileName, (nFileNameLen + 1), szFileName.c_str()); pFilePath = pFileName + nFileNameLen; while(pFilePath > pFileName) { if(*pFilePath == '.') { *pFilePath = '/0'; break; } pFilePath--; } while(*pFileName != '/0') { szNewFileName += *pFileName; pFileName++; } szNewFileName += "_new.exe"; return szNewFileName; } 文件复制 void createDestFile(string szSrcFileName, string szDestFileName) { ifstream ifs(szSrcFileName.c_str(), ios::binary); ofstream ofs(szDestFileName.c_str(), ios::binary|ios::out); ofs << ifs.rdbuf(); ifs.close(); ofs.close(); return; } 再发一个字符串替换的。 string& replace_all(string& szFullStr, const string& szPattern, const string& szFill) { string szTmpResult, szPrevStr, szRestStr; while(true) { int nPos = szFullStr.find(szPattern) ; if(nPos != string::npos) { szPrevStr = szFullStr.substr( 0, (nPos + szPattern.length()) ); szRestStr = szFullStr.substr( (nPos + szPattern.length()), szFullStr.length() ); szPrevStr.replace(nPos, szPattern.length(), szFill); szTmpResult += szPrevStr; szFullStr = szRestStr; } else { szTmpResult += szFullStr; break; } } szFullStr = szTmpResult; return szFullStr; } vs2008下通过调试