文件读写

读文件

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();
 }
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值