Click here to Skip to main content
15,907,492 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Native c++ using VC++ 8.0 Pin
hogan.john17-Jun-08 2:28
hogan.john17-Jun-08 2:28 
GeneralRe: Native c++ using VC++ 8.0 Pin
Cedric Moonen17-Jun-08 3:43
Cedric Moonen17-Jun-08 3:43 
GeneralRe: Native c++ using VC++ 8.0 Pin
Saurabh.Garg17-Jun-08 0:13
Saurabh.Garg17-Jun-08 0:13 
GeneralRe: Native c++ using VC++ 8.0 Pin
hogan.john17-Jun-08 2:29
hogan.john17-Jun-08 2:29 
GeneralRe: Native c++ using VC++ 8.0 Pin
Saurabh.Garg17-Jun-08 2:35
Saurabh.Garg17-Jun-08 2:35 
GeneralRe: Native c++ using VC++ 8.0 Pin
malaugh17-Jun-08 11:43
malaugh17-Jun-08 11:43 
GeneralRe: Native c++ using VC++ 8.0 Pin
Saurabh.Garg17-Jun-08 19:43
Saurabh.Garg17-Jun-08 19:43 
QuestionProblem Reading/Writing text/dat files Pin
Trupti Mehta16-Jun-08 21:52
Trupti Mehta16-Jun-08 21:52 
Hello,

I have serialized objects which I write in dat/txt files. While writing it doesn't throw any errors but I feel it writes only 1 record instead of 2 & while reading after reading 2, it doesn't stop. I might be missing something in reading case, as I don't know how to know the number of records in the file or how to trap when it should stop.

The code for storing the objects in a Map & calling a function to write objects to/from map file.


void COperatorDlg::OnBnClickedWriteBtn()   
{   
 // TODO: Add your control notification handler code here   
 CMap<int, int, OperatorDetails, OperatorDetails> newOperMap;   
  
 DbOperations db;   
 newOperMap.SetAt(1, OperatorDetails(1, _T("One")));   
 newOperMap.SetAt(2, OperatorDetails(2, _T("Two")));   
 db.WriteOperators(newOperMap);   
}   
  
  
void COperatorDlg::OnBnClickedReadBtn()   
{   
 // TODO: Add your control notification handler code here   
 CMap<int, int, OperatorDetails, OperatorDetails>* existOperMap;   
  
 DbOperations db;   
 existOperMap = db.ReadOperators();   
 CString s = _T("Read Operators : ");   
 s.Format(_T("%s %d"), s, existOperMap->GetCount());   
 AfxMessageBox(s);   
}   
  
  
DbOperations::DbOperations()   
{   
 operMap = new CMap<int, int, OperatorDetails, OperatorDetails>(10);   
 deptMap = new CMap<int, int, DeptDetails, DeptDetails>(10);   
}   
  
  
// Write Operators to the file   
void DbOperations::WriteOperators(CMap<int, int, OperatorDetails, OperatorDetails> &opdtMap) {   
 HANDLE hFile = CreateFile(OPER_FILE,   
      GENERIC_WRITE, FILE_SHARE_WRITE,   
         NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);   
  
 if (hFile == INVALID_HANDLE_VALUE)   
  AfxMessageBox(_T("Error OPeniong File"));   
 else {   
  CFile myfile(hFile);   
  int key;   
  OperatorDetails od;   
  
  CArchive ar(&myfile, CArchive::store);   
  
  for (POSITION pos = opdtMap.GetStartPosition(); pos != NULL;) {   
   opdtMap.GetNextAssoc(pos, key, od );    
   ar.WriteObject(&od);   
   od.~OperatorDetails();   
  }   
     
  od.~OperatorDetails();   
  ar.Close();   
  myfile.Close();   
 }   
  
 return;   
}   
  
// Read Operators from the file   
CMap<int, int, OperatorDetails, OperatorDetails>* DbOperations::ReadOperators()   
{   
 int count =0;   
 if(operMap->IsEmpty() == false)   
  operMap->RemoveAll();   
  
 HANDLE hFile = CreateFile(OPER_FILE,   
      GENERIC_READ, FILE_SHARE_READ,   
         NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);   
  
    
 if (hFile == INVALID_HANDLE_VALUE)   
  AfxMessageBox(_T("Error OPening File"));   
 else {   
  CFile myfile(hFile);   
  OperatorDetails* od;   
  OperatorDetails odObj;   
  
  // Start from start of the file   
  myfile.SeekToBegin();   
  CArchive ar(&myfile, CArchive::load);   
  
  
  while (true) {   
   od = (OperatorDetails*) ar.ReadObject( RUNTIME_CLASS(OperatorDetails) );    //************* I got to trap to stop   
   if (od == NULL)   
    break;   
      
   odObj.SetOperatorNo(od->GetOperatorNo());   
   odObj.SetOperName(od->GetOperName());   
//   ASSERT(odObj == *od);   
  
   count++;   
//     while ( (ar.Read(&dd, sizeof(dd))) != 0) {   
   operMap->SetAt(odObj.GetOperatorNo(), odObj);   
  }   
        
  CString s;   
  s.Format(_T("%d %s %d"), operMap->GetCount(), _T(" Count = "), count);   
  AfxMessageBox(s);   
  
  odObj.~OperatorDetails();   
  od->~OperatorDetails();   
  delete od;   
  myfile.Close();   
  myfile.~CFile();   
 }   
    
 return operMap;   
}   



Initially I had tried with dat only, since it wan't working so looked for text. But both provides same results. The text & dat file written is :
ÿÿ
 OperatorDetails Two

The Error basically says an attempt to read after the end of the file & then unhandled exception. Assert was throwing ASsertion Failure, so I replaced it with Set/Get methods.


I need help in solving the error. I believe in Read() my while loop is wrong. Can't figure out about Write(). The for loop runs for 2 times, takes both object details but the resulted output in file shows only first object that is the last one written in map & first read from map. Any help is highly appreciated. Would be great, if can provide help asap, as i am in a hurry now. Any improvement in the above code is also highly appreciated.

Thanks

Thanks

Terry

AnswerRe: Problem Reading/Writing text/dat files Pin
Mark Salsbery17-Jun-08 6:24
Mark Salsbery17-Jun-08 6:24 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta17-Jun-08 7:52
Trupti Mehta17-Jun-08 7:52 
GeneralRe: Problem Reading/Writing text/dat files Pin
Mark Salsbery17-Jun-08 8:16
Mark Salsbery17-Jun-08 8:16 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta17-Jun-08 22:28
Trupti Mehta17-Jun-08 22:28 
GeneralRe: Problem Reading/Writing text/dat files Pin
Mark Salsbery18-Jun-08 6:13
Mark Salsbery18-Jun-08 6:13 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta18-Jun-08 22:57
Trupti Mehta18-Jun-08 22:57 
GeneralRe: Problem Reading/Writing text/dat files Pin
Mark Salsbery18-Jun-08 9:12
Mark Salsbery18-Jun-08 9:12 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta18-Jun-08 22:44
Trupti Mehta18-Jun-08 22:44 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta19-Jun-08 0:15
Trupti Mehta19-Jun-08 0:15 
GeneralRe: Problem Reading/Writing text/dat files Pin
Mark Salsbery19-Jun-08 6:04
Mark Salsbery19-Jun-08 6:04 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta19-Jun-08 22:38
Trupti Mehta19-Jun-08 22:38 
AnswerRe: Problem Reading/Writing text/dat files Pin
ThatsAlok17-Jun-08 16:45
ThatsAlok17-Jun-08 16:45 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta17-Jun-08 22:33
Trupti Mehta17-Jun-08 22:33 
GeneralRe: Problem Reading/Writing text/dat files Pin
ThatsAlok18-Jun-08 5:15
ThatsAlok18-Jun-08 5:15 
GeneralRe: Problem Reading/Writing text/dat files Pin
Trupti Mehta18-Jun-08 22:48
Trupti Mehta18-Jun-08 22:48 
Questiona error about c++ read xml Pin
huke198716-Jun-08 21:31
huke198716-Jun-08 21:31 
GeneralRe: a error about c++ read xml Pin
huke198716-Jun-08 22:01
huke198716-Jun-08 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.