我使用矢量寫入數據(結構)轉換成文件,當我嘗試使用矢量迭代器檢索數據,這讓我讀:"Vector iterator is not dereferenceable."
矢量迭代器不dereferencable,同時試圖從文件
這是我的代碼:
void CProgram_1_STLDlg::OnBnClickedBtnView()
{
// TODO: Add your control notification handler code here
CFile file;
CFileException e;
studentVector::iterator sit;
studentVector::iterator sBegin = sVector.begin();
studentVector::iterator sEnd = sVector.end();
CString path = _T("D:\\Student.txt");
if (file.Open(path, CFile::modeRead, &e)) {
while (file.Read((char *)&sVector, sizeof(sVector))) {
AfxMessageBox(_T("File opened in read mode."), MB_ICONINFORMATION);
AfxMessageBox(_T("ID:\t")+sit->id+L"\nName:\t"
+sit->name+L"\nMarks:\t"+sit->marks+L
"\nPercentage:\t"+sit->per+L"\nState:\t"+sit->state);
sit++;
}
//file.Read((char *)&sData, sizeof(sData));
/*for (sIterator = sVector.begin(); sIterator != sVector.end(); sIterator++) {
//AfxMessageBox(_T("ID:\t")+sIterator->id+L
"\nName:\t"+sIterator->name+L"\nMarks:\t"
+sIterator->marks+L"\nPercentage:\t"+sIterator->per+L
"\nState:\t"+sIterator->state);
//AfxMessageBox(_T("Hello..Testing...!"));
}
*/
} else {
AfxMessageBox(_T("Error! Unable to open file."), MB_ICONERROR);
}
}
現在我不知道如何解決這個錯誤。
注意:我提到的一些鏈接是谷歌給我的,但我無法解決我的問題。
'sIterator!= sit' – SHR
@SHR我這樣做了,但是有一兩件事我不知道是怎麼計劃將知道有文件結尾,這就是爲什麼我用while循環來讀取文件,並採取其con帳篷到矢量中。 –