C++读取文本数据

BOOL CDrawQCDlg::GetData(int xclo_number,int yclo_number) //读取数据(txt格式): x,y数据均按列存储
{
 int     col_size = 0 ;  
    int     row_size = 0 ;  
 CFile   file ;  

 InitData();

 if (file.Open("data.txt", CFile::modeRead))  
 {  
    CString text = ReadLine(file) ; // get a line from the file   
    // MessageBox(text);
    CString token, data ;  
    // count how many elements across their are   
    int pos = 0 ;  
    while (pos < text.GetLength())  
    {  
     pos = GetStringToken(text, token, pos, '/t') ;  //数据的分隔符:,or 空格 or tab?
     col_size++ ;  
    }  
    // allocate an array to hold the data   
    double  *pData = new double[col_size] ;  
    // CMatrix m(col_size, 1) ;                    // create a 1 row matrix which we will concatinate rows to   
    int j =0, k1=0, k2=0;
    while (text.GetLength() > 0)  
    {  
     int i = 0;  
     pos = 0 ;  
     while (pos < text.GetLength())  
     {  
      pos = GetStringToken(text, token, pos, '/t') ;  
      pData[i++] = atof(token) ; 
      if (i == xclo_number+1)
      {
       Xaxis.Add(token);
       lengthx[k1++]=token.GetLength();
      }
      if (i == yclo_number+1)
      {
       Yaxis.Add(token);
       lengthy[k2++]=token.GetLength();
      }
     }  
     ASSERT(i == col_size) ;  
     text = ReadLine(file) ;  
     Xdata[j] = pData[xclo_number];
     Ydata[j] = pData[yclo_number];
     j++;  
     row_size++ ;  
     datanumber = j;
    }  
    delete []pData ;  
    file.Close() ;   
    return TRUE;
 }       
 else
  return false;
}

// read a line of text from the current file   
CString CDrawQCDlg::ReadLine(CFile& file)
{
  CString line("") ;  
  char    ch[3] ;  
  DWORD   hBytesRead  = 0 ;  
  
  while (true)  
        {  
   hBytesRead = file.Read(&ch[0], 2) ;  
   if (hBytesRead == 2)  
    file.Seek(-1L, CFile::current) ;          
   if (hBytesRead == 0)  
    break ;                 // end of file   
   if (ch[0] == '/n')  
            {  
    if (ch[1] == '/r')  
     file.Seek(1L, CFile::current) ;  
    break ;  
            }  
   if (ch[0] == '/r')  
            {  
    ch[0] = '/n' ;  
    if (ch[1] == '/n')  
     file.Seek(1L, CFile::current) ;  
    break ;  
            }  
   if (ch[0] != '/015')  
            {  
    // ignore LF characters   
    ch[1] = '/0' ;  
    line += ch ;  
            }  
        }  
  return line ;  
}

int CDrawQCDlg::GetStringToken(CString source, CString &destination, int start, char ch)
{
  ASSERT(start >= 0) ;  
  
  // at the end of the source string ?   
  if (start >= source.GetLength())  
        {  
   destination = "" ;                  // no token available   
   return source.GetLength() ;         // return @ end of string   
        }  
  
  // skip past any termination characters at the start position   
  while (start < source.GetLength())  
        {  
   if (ch == source[start])  
    start++ ;  
   else  
    break ;  
        }  
  // find the next occurance of the terminating character   
  int pos = source.Find(ch, start) ;          // find termination character   
  if (pos < 0)  
        {  
   // terminator not found, just return the remainder of the string   
   destination = source.Right(source.GetLength() - start) ;  
   return source.GetLength() ;  
        }  
  // found terminator, get sub string   
  destination = source.Mid(start, pos - start) ;  
  return pos ;  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值