文件操作

来源:互联网 发布:淘宝货到付款拒收后果 编辑:程序博客网 时间:2024/06/03 20:37
 1.

void append(const spi::InternalLoggingEvent& event)
 {
  if(!out.good())
  {
   getErrorHandler()->error(  LOG4CPLUS_TEXT("file is not open: ")
    + filename);
   return;
  }

  if(out.tellp() >= m_maxFileSize)
  {
   if (!IsDiskNotEnough())
   {// 如果磁盘没有满就新建一个日志文件
    ++m_nLastMaxNo ;
    CreateNewLogFile ();
    layout->formatAndAppend(out, event);

    if(immediateFlush)
    {
     out.flush();
    }
   }
  }
  else
  {
   layout->formatAndAppend(out, event);

   if(immediateFlush)
   {
    out.flush();
   }
  }
  
 }

2.ofstream outfile2("file1", ofstream::out | ofstream::trunc) ;

3.每次写文件不是接着写,而是先清空上一次的再写   ofstream outfile2("file1", ofstream::trunc) ;

4.写字符串    file << stringTemp ;   或者 file << stringTemp.c_str() ;

5.可以向文件六种直接写入数字 如:    file << 12 << endl ;

6.指定文件的写入位置: ofTemp.seekp(0) ;

7.刷新文件:paraOfstream.flush() ;

8.同时读取两个文件,并读取:

std::streampos posIpTemp = if_IPTemp.tellg() ;
    std::streampos posCityTemp = if_city.tellg() ;
    while (getline(if_IPTemp, strIPTempFrom) && getline(if_city, strCityFrom))
    {
        /*if (strIPTempFrom[0] == '2')
        {
            int k = 0 ;
        }*/
        std::string strCityBigger = strCityFrom.substr(strCityFrom.find("\t") + 1, strCityFrom.rfind("\t") - strCityFrom.find("\t") - 1) ;
        std::string strIPLitter = strIPTempFrom.substr(0, strIPTempFrom.find("\t")) ;
        
        if (_atoi64(strIPLitter.c_str()) > _atoi64(strCityBigger.c_str()))
        {
            of_city << strCityFrom << "\r\n" ;
            if_IPTemp.seekg(posIpTemp) ;
        }
        else
        {
            of_city << strIPTempFrom << "\r\n" ;
            if_city.seekg(posCityTemp) ;
        }

        posIpTemp = if_IPTemp.tellg() ;
        posCityTemp = if_city.tellg() ;
    }