Hi,
I am trying to use strtok()..the idea is to split the text in tokens seperated by ";" as the delimiter. The text is in two parts like:
apple;mango (apple being the first part and mango being the second I want to split into).
The code I'm using:
#include<stdio.h>
#include<string.h>
int main()
{
char read[100];
FILE *at_mmi;
char *result ;
char delim[] = ";";
char read_AT[40];
char read_MMI[40];
memset( read_AT, 0 , sizeof(read_AT) );
memset( read_MMI, 0 , sizeof(read_MMI) );
memset( read, 0, sizeof( read ) );
at_mmi = fopen("C:/Documents and Settings/My Documents/AT_MMI.txt","r"); //Open the file containing the AT and MMI List
if( at_mmi == NULL )
{
printf("Error opening the file.");
exit(0);
}
while( !feof( at_mmi )) // Check for End of File
{
fgets(read,100,at_mmi);
result = strtok(read, delim); // Read the AT Command part from txt file
strcpy(read_AT,result);
printf("\n%s",read_AT);
//printf("\n%s",result);
result = strtok(NULL, delim); //Read the MMI Command part from txt file
strcpy(read_MMI,result);
printf("\n%s",read_MMI);
}
fclose(at_mmi);
getchar();
return 0;
The problem is that the program runs but then it gives this error:
Unhandled exception at 0x1026f693 in test2.exe: 0xC0000005: Access violation reading location 0x00000000.
I looked around the problem seems to me that the second time I use strtok() its returning a null pointer. But then how do I save the second part of the text into another buffer/array. Also, it prints only the first part of the last line in the file again at the end.
Any pointers would be great. Thanks..