VC++中文本文件的读写及处理

 
        在View中添加了以下两个函数,第一个函数用于逐行读出Text文件中的以逗号隔开的数据,并进行简单的处理——使得每行中第一个逗号前的数据以1为后缀,第二个逗号前的数据以2为后缀,以此类推;第二个函数用来将处理后的结果保存到一个新的文件中。
std::vector<CString> CDataPreprocessView::DataPreprocess()
{
      
       std::vector<CString> vecResult;
      
    CStdioFile fsin;
    CString str = "";
       CString strTemp, intToStr;
       int iCommaPosition, iItemCount;
    fsin.Open("D://Visual Workspace//Data Mining//DataPreprocess//mushrooms.txt", CFile::modeReadWrite | CFile::typeText);// Open text file
    while(fsin.ReadString(str)) // read data line by line
       {   iItemCount = 1;
              strTemp = "";
              intToStr = "";
              while((iCommaPosition = str.Find(",")) != -1)
              {//find each comma, and add a number as the suffix of the data before the comma
                     intToStr.Format("%d", iItemCount);
                     strTemp = strTemp + str.Left(iCommaPosition) + intToStr + ",";
                     str = str.Right(str.GetLength() - iCommaPosition - 1);
                     iItemCount++;
              }
              intToStr.Format("%d", iItemCount);
              strTemp += str + intToStr + "/n"; 
 
              vecResult.push_back(strTemp);//temporary store each line processed into a vector
       }
       fsin.Close();// close the file
       return vecResult;
}
// function for saving data into a certain file
void CDataPreprocessView::OnFileSave()
{
       CStdioFile fsout;
       std::vector<CString> vecData;
       std::vector<CString>::iterator it_vecData;
       vecData = DataPreprocess();
       it_vecData = vecData.begin();
       fsout.Open("D://Visual Workspace//Data Mining//DataPreprocess//mushroomsProcessed.txt", CStdioFile::modeReadWrite | CStdioFile::typeText); // open a file for saving data
 
       while(it_vecData != vecData.end())
       {            //write each line in the vector into the file
              fsout.WriteString(*it_vecData);
             
              it_vecData++;
       }
 
       fsout.Close();// close the file
      
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值