NarayanaMurty
September 5th, 2002, 04:49 PM
Hello All,
I am using strtok() function to get the next token available from a string.
I am reding a file using memory mapping and storing the data in a char* buffer. from this buffer I am getting line by line using strtok() function by putting the token delimiters as "\r\n". I am having an error file that is not ended with the above delimeters as it is error file. strtok is crashing with this file at the last statement.
Is there any problem with strtok()? or with my code? Can any one have any idea? If so what is the solution for this?
I am sending the file as an attachment. If I try to change the contents and save it is working fine. So use this file without altering.
My code:
HANDLE hFile;
HANDLE hMap;
char* chFileData;
hFile = CreateFile("C:\\Temp\\temp.001",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
DWORD fSize = GetFileSize(hFile,NULL);
hMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,fSize,NULL);
int nError = 0;
nError = GetLastError();
if ((hMap != NULL) && (nError == ERROR_ALREADY_EXISTS))
{
CloseHandle(hMap);
hMap = NULL;
return FALSE;
}
else if (hMap == NULL) //ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND
{
CloseHandle(hFile);
hFile = NULL;
hMap = NULL;
return FALSE;
}
chFileData = (char*)MapViewOfFile(hMap,FILE_MAP_COPY,0,0,0);
char* chBuffer = strtok(chFileData,"\r\n");
while (chBuffer != NULL)
{
//AfxMessageBox(chBuffer);
chBuffer = strtok(NULL,"\r\n");
}
if(hFile!=NULL)
{
CloseHandle(hFile);
hFile = NULL;
}
if(hMap!=NULL)
{
CloseHandle(hMap);
hMap = NULL;
}
if((chFileData != "") || (chFileData != NULL))
{
UnmapViewOfFile(chFileData);
chFileData = NULL;
}
I am using strtok() function to get the next token available from a string.
I am reding a file using memory mapping and storing the data in a char* buffer. from this buffer I am getting line by line using strtok() function by putting the token delimiters as "\r\n". I am having an error file that is not ended with the above delimeters as it is error file. strtok is crashing with this file at the last statement.
Is there any problem with strtok()? or with my code? Can any one have any idea? If so what is the solution for this?
I am sending the file as an attachment. If I try to change the contents and save it is working fine. So use this file without altering.
My code:
HANDLE hFile;
HANDLE hMap;
char* chFileData;
hFile = CreateFile("C:\\Temp\\temp.001",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
DWORD fSize = GetFileSize(hFile,NULL);
hMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,fSize,NULL);
int nError = 0;
nError = GetLastError();
if ((hMap != NULL) && (nError == ERROR_ALREADY_EXISTS))
{
CloseHandle(hMap);
hMap = NULL;
return FALSE;
}
else if (hMap == NULL) //ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND
{
CloseHandle(hFile);
hFile = NULL;
hMap = NULL;
return FALSE;
}
chFileData = (char*)MapViewOfFile(hMap,FILE_MAP_COPY,0,0,0);
char* chBuffer = strtok(chFileData,"\r\n");
while (chBuffer != NULL)
{
//AfxMessageBox(chBuffer);
chBuffer = strtok(NULL,"\r\n");
}
if(hFile!=NULL)
{
CloseHandle(hFile);
hFile = NULL;
}
if(hMap!=NULL)
{
CloseHandle(hMap);
hMap = NULL;
}
if((chFileData != "") || (chFileData != NULL))
{
UnmapViewOfFile(chFileData);
chFileData = NULL;
}