关闭

文件读写

853人阅读 评论(0) 收藏 举报

读文件

spAttriStyleInfo AttributeFile::ReadPointAttrFile(const string& filename)
{
 spAttriStyleInfo spVO(new AttriStyleInfo());
 ifstream ifs;
 /*ifs.exceptions ( ifstream::eofbit | ifstream::badbit | ifstream::failbit );*/
 try {
  ifs.open(filename.c_str());
  if (!ifs.is_open()) {
   throw ArdError(ArdError::FILE_OPEN_FAILURE);
  }

  typedef istream_iterator<string> string_input;
  vector<string> dictionary;
  copy(string_input(ifs), string_input(), back_inserter(dictionary));
  vector<string>::iterator iter;
  iter = std::find(dictionary.begin(), dictionary.end(), "[POINT]");
  string strCmt = "";
  while (iter != dictionary.end())
  {
   string strID = (++iter)->substr(3);
   unsigned char cPrepresentId  = atoi((++iter)->substr(6).c_str());
   int iSize      = atoi((++iter)->substr(5).c_str());
   BYTE RValue    = atoi((++iter)->substr(4).c_str());
   BYTE GValue    = atoi((++iter)->substr(6).c_str());
   BYTE BValue    = atoi((++iter)->substr(5).c_str());
   //Gdiplus::Color color = RGB(RValue, GValue, BValue);
   if(!(++iter)->empty())
   strCmt = (iter)->substr(8);
   AttriStyleDef pointAttr(strID, Gdiplus::Color(RValue, GValue, BValue), cPrepresentId, strCmt, iSize);
   iter = find(iter, dictionary.end(), "[POINT]");

   spVO->addPointAttriStyle(pointAttr);
  } 
 }
 catch (ios::failure e)
 {
  ios_base::iostate s = ifs.rdstate();

  if ( s & ifstream::eofbit )
  {
   throw ArdError(ArdError::FILE_READ_FAILURE, &e);
  }
  if ( s & ifstream::badbit )
  {
   throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
  }
  if ( s & ifstream::failbit )
  {
   throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
  }
 }

 if (ifs.is_open())
 {
  ifs.close();
 }
 return spVO;
}

写文件

/**
* write point AR/D attributes to .att file
*/
void AttributeFile::WritePointAttrFile(const string& filename, spAttriStyleInfo attributes)
{
 ofstream outFile;
 outFile.exceptions ( ifstream::eofbit | ifstream::badbit | ifstream::failbit );
 try{
  outFile.open(filename.c_str());
  if (!outFile.is_open())
  {
   throw ArdError(ArdError::FILE_OPEN_FAILURE);
  }
  map<std::string, AttriStyleDef> pointAttributes = attributes->GetPointMap();
  map<std::string, AttriStyleDef>::iterator iterPoint;

  for (iterPoint = pointAttributes.begin(); iterPoint != pointAttributes.end(); ++iterPoint)
  {
   AttriStyleDef spvo = iterPoint->second;
   string iID         = spvo.GetId();
   unsigned int cPrepresentId  = spvo.GetRepresentId();
   int iSize          = spvo.GetSize();
   int RValue        = spvo.GetColor().GetR();
   int GValue        = spvo.GetColor().GetG();
   int BValue        = spvo.GetColor().GetB();
   string strCmt      = spvo.GetCmt();

   outFile << "[POINT]" <<'/n'<< "ID="    << iID     << '/n' <<
    "STYLE="   << cPrepresentId  << '/n' << "SIZE="    << iSize  << '/n' <<
    "Red="     << RValue         << '/n' << "Green="   << GValue << '/n' <<
    "Blue="    << BValue         << '/n' << "COMMENT=" << strCmt << endl;
  }
 }
 catch (ios::failure e)
 {
  ios_base::iostate s = outFile.rdstate();

  if ( s & ifstream::eofbit )
  {
   throw ArdError(ArdError::FILE_READ_FAILURE, &e);
  }
  if ( s & ifstream::badbit )
  {
   throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
  }
  if ( s & ifstream::failbit )
  {
   throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
  }
 }
 if (outFile.is_open())
 {
  outFile.close();
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33126次
    • 积分:536
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:5篇
    • 评论:4条
    最新评论