Prob. to catch the EOF ?!
-
hi all, in one program i need to extract data from a txt file. the program extracts data line by line, but it doesn't catch the EOF. i included the part of the code that gives me trouble below, if anybody knows how to fix this, let me know! thanx Fred ... char c = 0, *buffer = 0; int nc = 0; while(c != EOF) // !! PROB { if(!ReadFile(hFile, &c, 1, &dwByteRead, NULL)) { AfxMessageBox("Error in ReadFile() : %d", GetLastError()); return NULL; } if(dwByteRead == 1) { if(c == '\r' || c == '\n' || nc >= CHAR_LINE) { szSentence[nc] = 0; return szSentence; } else { szSentence[nc++] = c; } } }
-
hi all, in one program i need to extract data from a txt file. the program extracts data line by line, but it doesn't catch the EOF. i included the part of the code that gives me trouble below, if anybody knows how to fix this, let me know! thanx Fred ... char c = 0, *buffer = 0; int nc = 0; while(c != EOF) // !! PROB { if(!ReadFile(hFile, &c, 1, &dwByteRead, NULL)) { AfxMessageBox("Error in ReadFile() : %d", GetLastError()); return NULL; } if(dwByteRead == 1) { if(c == '\r' || c == '\n' || nc >= CHAR_LINE) { szSentence[nc] = 0; return szSentence; } else { szSentence[nc++] = c; } } }
ReadFile
does not return anyEOF
. Instead, it returnsTRUE
withdwByteRead
being 0. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
hi all, in one program i need to extract data from a txt file. the program extracts data line by line, but it doesn't catch the EOF. i included the part of the code that gives me trouble below, if anybody knows how to fix this, let me know! thanx Fred ... char c = 0, *buffer = 0; int nc = 0; while(c != EOF) // !! PROB { if(!ReadFile(hFile, &c, 1, &dwByteRead, NULL)) { AfxMessageBox("Error in ReadFile() : %d", GetLastError()); return NULL; } if(dwByteRead == 1) { if(c == '\r' || c == '\n' || nc >= CHAR_LINE) { szSentence[nc] = 0; return szSentence; } else { szSentence[nc++] = c; } } }
From MSDN on
ReadFile()
.the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF
when the file pointer goes beyond the current end of file.So, every time you get to EOF, you're generating what you perceive to be an error. Check for both instead of just the return value. Alternatively, you could use
c
to check for the value of 46 (0x2E), as that's the ASCII/ANSI EOF char. Jeremy L. Falcon "It's in the mail." Homepage : Sonork = 100.16311
Surely some striving souls survive symptomatic stress?