scanning until the EOF
-
Hi all, I have the following code: // CODE STARTS HERE // int i,j; std::fstream f_load; char appoggio[20]; // carica il nan oppure il valore numerico CString percorso; CFileDialog fd(TRUE, NULL, percorso, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY); fd.m_pOFN->lpstrTitle = "Carica file di osservazioni"; if (fd.DoModal() == IDOK) { percorso = fd.GetPathName(); UpdateData(false); f_load.open(percorso,std::ios::in); int contatore = 0; do{ f_load>>appoggio; contatore++; } while(f_load!=EOF); } // CODE ENDS HERE // but there's something wrong in the while condition. Which is the right while condition to scan the file until its end ? Thanx in advance, Desmo16.
-
Hi all, I have the following code: // CODE STARTS HERE // int i,j; std::fstream f_load; char appoggio[20]; // carica il nan oppure il valore numerico CString percorso; CFileDialog fd(TRUE, NULL, percorso, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY); fd.m_pOFN->lpstrTitle = "Carica file di osservazioni"; if (fd.DoModal() == IDOK) { percorso = fd.GetPathName(); UpdateData(false); f_load.open(percorso,std::ios::in); int contatore = 0; do{ f_load>>appoggio; contatore++; } while(f_load!=EOF); } // CODE ENDS HERE // but there's something wrong in the while condition. Which is the right while condition to scan the file until its end ? Thanx in advance, Desmo16.
In your loop, you are checking if the EOF has been reached at the end of the loop, but the pointer has already been increased causing your code not to go through the loop for the final time. You should test before entering the loop, ie
while (f_load != EOF) { // do your loop }
-
In your loop, you are checking if the EOF has been reached at the end of the loop, but the pointer has already been increased causing your code not to go through the loop for the final time. You should test before entering the loop, ie
while (f_load != EOF) { // do your loop }
-
I don't think it's that the matter, because the compiler tells me it cannot convert an int (the EOF) into a std::fstream (the f_load)
Sorry, been a while since I used it. It think it should be
while (!f_load.eof() ) { }