C++读取文件所有内容+写一个新文件
2011-07-26 10:55
483 查看
1. ifstream myFile ;
CStdString strForbiddenFilePathName = strModulePathName + "forbbiden.dat" ;
myFile.open(strForbiddenFilePathName.c_str()) ;
if (!myFile)
{
g_Log.GetLog()->WriteDBGLog(IMP_RECORD, "CWebPageAnalyze::GetArtical", strPreLog + "打开禁用关键词失败");
return FALSE ;
}
else
{
while(getline (myFile, strTemp))
{
arrWebForbiddenWord.push_back(strTemp) ;
strTemp.clear() ;
}
}
myFile.close() ;
myFile.clear() ;
2. // 注:下面的代码中CStdString不是C++的内置类型,如果要复制,请相应更改成string等。有些函数是本人自己写的,也要相应删除。
ifstream myFile1 ;
string strTemp1 ;
CStdString strUrl = strModulePathName + "url.txt" ;
myFile1.open(strUrl.c_str()) ;
ofstream myFileOut1;
CStdString strExtractContent = strModulePathName + "ExtractContent.txt" ;
myFileOut1.open(strExtractContent.c_str()) ;
strTemp1.clear() ;
while (getline(myFile1, strTemp1))
{
TestAnalyze.GetArtical(strTemp1, strMyTitle, strMyContent) ;
myFileOut1<<strTemp1.c_str()<<endl<<strMyTitle.c_str()<<"##"<<endl<<strMyContent.c_str()<<endl<<"####"<<endl ;
strTemp1.clear() ;
strMyTitle.clear() ;
strMyContent.clear() ;
}
myFile1.close() ;
myFile1.clear() ;
myFileOut1.close() ;
myFileOut1.clear() ;
另一个例子:
ofstream a ;
a.open ("1.txt") ;
DWORD I = GetTickCount () ;
for (int i =0; i<10000; ++i)
a<< 2011 << "-" << 8 << "-" << 3 << " " << 14 << ":" << 22 << ":" << 59
<< "\t" << "172.17.17.11" << "\t" << "49801bcf518c445 " << "\t" << "logvertest.exe"
<< "\t" << "Administrator" << "\t" << "无" << "\t" << "Run"
<< "\t" << "neirong" << "\t" << "Warn" << "\r\n";
DWORD II = GetTickCount () - I ;
注意:直接写数字的花费的时间比写字符的快。如 写8 比 写"08"快。
CStdString strForbiddenFilePathName = strModulePathName + "forbbiden.dat" ;
myFile.open(strForbiddenFilePathName.c_str()) ;
if (!myFile)
{
g_Log.GetLog()->WriteDBGLog(IMP_RECORD, "CWebPageAnalyze::GetArtical", strPreLog + "打开禁用关键词失败");
return FALSE ;
}
else
{
while(getline (myFile, strTemp))
{
arrWebForbiddenWord.push_back(strTemp) ;
strTemp.clear() ;
}
}
myFile.close() ;
myFile.clear() ;
2. // 注:下面的代码中CStdString不是C++的内置类型,如果要复制,请相应更改成string等。有些函数是本人自己写的,也要相应删除。
ifstream myFile1 ;
string strTemp1 ;
CStdString strUrl = strModulePathName + "url.txt" ;
myFile1.open(strUrl.c_str()) ;
ofstream myFileOut1;
CStdString strExtractContent = strModulePathName + "ExtractContent.txt" ;
myFileOut1.open(strExtractContent.c_str()) ;
strTemp1.clear() ;
while (getline(myFile1, strTemp1))
{
TestAnalyze.GetArtical(strTemp1, strMyTitle, strMyContent) ;
myFileOut1<<strTemp1.c_str()<<endl<<strMyTitle.c_str()<<"##"<<endl<<strMyContent.c_str()<<endl<<"####"<<endl ;
strTemp1.clear() ;
strMyTitle.clear() ;
strMyContent.clear() ;
}
myFile1.close() ;
myFile1.clear() ;
myFileOut1.close() ;
myFileOut1.clear() ;
另一个例子:
ofstream a ;
a.open ("1.txt") ;
DWORD I = GetTickCount () ;
for (int i =0; i<10000; ++i)
a<< 2011 << "-" << 8 << "-" << 3 << " " << 14 << ":" << 22 << ":" << 59
<< "\t" << "172.17.17.11" << "\t" << "49801bcf518c445 " << "\t" << "logvertest.exe"
<< "\t" << "Administrator" << "\t" << "无" << "\t" << "Run"
<< "\t" << "neirong" << "\t" << "Warn" << "\r\n";
DWORD II = GetTickCount () - I ;
注意:直接写数字的花费的时间比写字符的快。如 写8 比 写"08"快。
相关文章推荐
- c++将一个文件夹下的所有文件读取出来放在一个文件中
- windows下c++读取一个目录下的所有文件
- 第二周第四天(解析/过滤文件/找d盘里的所有txt文件/FileInputStream/FileOutputStream/拷贝文件/readLine读取一个文本行/在一个文档里写入内容)
- [QTP]通过vbscript读取一个文件中的所有内容
- C++ 如何将一个文件夹中的所有文件(.txt或者.log)内容整合到一个文件(.txt或者.log)当中
- C++ Demo 001: 读取一个从命令行指定的文件,输出文件内容到显示器,并且给每一行输出添加一个行号
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- C++ 读取文件所有内容的方法
- IO流_合并流读取两个文件的内容复制到一个文件中
- 修改linux下某一个文件夹下所有文件内容
- php读取一个文件内容
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
- C++ 读取文件内容到指定类型的变量
- shell读取一个文件内容到变量并逐一赋值
- 今天在ASP.NET上在写一个读取EXCEL文件内容的功能,但是IE8貌似把文件路径给屏蔽了,在IE6下面读取正常!有啥好办法?
- C/C++下读取某一文件夹下所有文件的方法
- c++ 如何将一个文件夹里的所有文件追加到另一个文件中
- python读取一个目录下所有txt里面的内容
- C++写的一个读取CSV文件的函数
- ubuntu C++ 读取文件夹下所有文件的文件名