我把讀取文件的代碼寫在Dlg的OnInitDialog()函數裡
當那個data文件只有一行的時候讀取文件是可以正常運行的,只在後面有問題。
但data文件有兩行的話就會停在getdata函數這裡
CStringData* GetData() const throw()
{
return( reinterpret_cast< CStringData* >( m_pszData )-1 );
}
讀文件代碼如下:
CStdioFile myFile;
CFileException fileException;
if (myFile.Open(_T("data.txt"), CFile::typeText | CFile::modeReadWrite), &fileException)
{
myFile.SeekToBegin();
CString str;
while (myFile.ReadString(str)){
single* fi = new single();
int sec = 0;
CString p;
for (int i = 0; i < str.GetLength(); i++){
if (str[i] == ','){
if (sec == 0) //單號
fi->setNum(p);
else if (sec == 1){
if (p == "0") fi->setCategory(0);
else fi->setCategory(1);
}
else if (sec == 2)
fi->setSendAddr(p);
else if (sec == 3)
fi->setSendPhone(p);
else if (sec == 4)
fi->setSendNote(p);
else if (sec == 5)
fi->setReceiveAddr(p);
else if (sec == 6)
fi->setReceivePhone(p);
else if (sec == 7)
fi->setReceiveNote(p);
else if (sec == 8)
fi->setSendDate(p);
else if (sec == 9)
fi->setReceiveDate(p);
else if (sec == 10){
if (p == "0")
fi->setReceived(false);
else fi->setReceived(1);
}
else if (sec == 11){
if (p == "0")
fi->setUnknow(0);
else
fi->setUnknow(1);
}
else if (sec == 12){
int num = _ttoi(p);
fi->setUnknowNote(num);
}
else{
double mon = _wtof(p);
fi->setMoney(mon);
sec = -1;
}
p = "";
sec++;
}
else p += str[i];
}
tree->insert(fi);
}
}
else
{
TRACE("Can't open file %s,error=%u\n", _T("data.txt"), fileException.m_cause);
}
myFile.Close();
求指教,這樣寫哪裡有錯嗎?
當文件裡有兩行數據的時候斷點調試下看看前一次讀取的數據是否都正確,還有第二次確定下是卡在了哪裡,看下堆棧調用窗口