the*_*der 3 c++ vector visual-c++
我正在使用向量将数据(结构)写入文件,当我尝试使用向量迭代器检索数据时,它给了我: "Vector iterator is not dereferenceable."
这是我的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道如何解决这个错误.
注意:谷歌给我的一些链接,但是我无法解决我的问题.
你不能简单地覆盖向量的内存.这几乎可以保证破坏你的过程.
此外,你永远不会分配任何东西sit,但期望它包含一些合理的东西.
您需要解析Student.txt中的数据并使用vector的成员函数来填充合理的数据.赋值可能会告诉您文件的外观,以便您可以解析它.