vc文件读写,用fstream和CStdioFile
2010-12-19 23:50
363 查看
vc文件读写,用fstream和CStdioFile。
第一种,用fstream:
另一种,使用CStdioFile,主要代码如下(完整源码):
界面如下:
第一种,用fstream:
#include <afx.h> #include <iostream> #include <fstream> using namespace std; // 如srcFile=D:\1.sql,则得到的destFile=D:\1_Result.sql void GetResultFileName(char * destFile, char * srcFile) { char *p; p = strrchr(srcFile, '\\'); if(p) { memcpy(destFile, srcFile, p - srcFile + 1); srcFile = p + 1; } p = strrchr(srcFile, '.'); if(p) { strncat(destFile, srcFile, p-srcFile); strcat(destFile, "_Result"); strcat(destFile, p); } else { strcat(destFile, srcFile); strcat(destFile, "_Result"); } } // 将srcString中的字符串替换后,写入outFile指向的文件中 void Replace(ofstream & outFile, char *srcString) { char destString[1024] = {0}; char *p, *pend, *q, *start; p = pend = q = start = NULL; int index = -1; while(srcString) { if(p = strstr(srcString, "to_date('")) { strncat(destString, srcString, p-srcString); // 拷贝to_date前面的字符串 p += 8; q = strstr(p, "'),"); // 搜索子字符串 pend = strchr(p+1, '\''); // 搜索特定字符 strncat(destString, p, pend-p+2); srcString = q + 3; } else { strcat(destString, srcString); break; } } outFile<<destString<<endl; } void Transform(char *destFile, char * srcFile) { ifstream inFile(srcFile); ofstream outFile(destFile); if(!inFile && !outFile) { cout<<"Open File["<<srcFile<<"] Failed."<<endl; return; } char line[1024]; while(inFile.getline(line, 1024)) { // cout<<"#"<<line<<"#"<<strlen(line)<<endl; Replace(outFile, line); } inFile.close(); outFile.close(); } void main() { char srcFile[] = "D:\\11.sql"; char destFile[100] = {0}; GetResultFileName(destFile, srcFile); Transform(destFile, srcFile); }
另一种,使用CStdioFile,主要代码如下(完整源码):
/* 功能:将srcFile中所有的to_date('x', 'y')转换为'x', 并将时间改为格式:年-月-日 hh24:mi:ss @destFile: 输出文件全路径 @srcFile: 输入文件全路径 */ void CFileReplaceDlg::Transform(CString destFile, CString srcFile) { CStdioFile inFile(srcFile, CStdioFile::modeRead); CStdioFile outFile(destFile, CStdioFile::modeCreate | CStdioFile::modeWrite); CString line; while(inFile.ReadString(line) != NULL) { Replace(outFile, line); } inFile.Close(); outFile.Close(); }
界面如下:
![](http://pic002.cnblogs.com/images/2010/91358/2010122000072833.jpg)
相关文章推荐
- VC读写TXT文件和CStdioFile读写乱码
- vc中unicode读写文件CStdioFile
- VC读写TXT文件和CStdioFile读写乱码----本人亲自测试可行
- vc下文件的创建--使用fstream和CFile
- C++文件读写详解(ofstream,ifstream,fstream)
- VC INI文件读写
- C++文件读写详解(ofstream,ifstream,fstream)
- VC配置文件ini的读写(转载)
- VC++实现host文件读写
- C++文件读写详解(ofstream,ifstream,fstream)
- 通过vc中的CDatabase类来直接通过ODBC读写Excel表格文件
- 使用CStdioFile读写文件
- VC++ 读写注册表,注册文件图标关联
- C++文件读写详解(ofstream,ifstream,fstream)
- C++文件读写详解(ofstream,ifstream,fstream)
- vc读写配置文件ini
- 在VC中读写ini配置文件
- 入门计划->使用(C++库)fstream读写文件
- C++文件读写详解(ofstream,ifstream,fstream
- [转][VC/MFC]关于内存映射文件技术处理大文件的读写-例