您的位置:首页 > 编程语言 > C语言/C++

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"快。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ string c
相关文章推荐