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