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..

while( !feof( at_mmi )) //  Check for End of File
	{
		fgets(read,100,at_mmi);
.
.

You should use:

while( fgets(read,100,at_mmi);) //  Read till available

This code worked fine for me.

char read[]="apple;mango";
char delim[]=";";
char *result;
char read_MMI[40],read_AT[40];
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);

Thanks for the reply...The thing is the change in the while condition that you mention in the above post is ok if the txt file has only one line..however that is not the case..it has multiple lines..if I use fgets in the while condition I think the loop will end after the first line itself..anyway..my bad..I should have been clearer..so the unhandled exception error still remains..

..if I use fgets in the while condition I think the loop will end after the first line itself

What made you think that?

I thought that fgets() reads till the end of line only if you don't mention the end of file condition as well..but now checkd it out:

fgets(): Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a '\0' character is appended to end the string.

thanks for the pointer..problem has been solved though..it was a problem with the text file..didn't think of checking that..thanks anyway...

>>Reading stops when a newline character is found
Yes but it is inside a loop so gets called again since eof isn't detected.

>>at end-of-file or error
Here it returns NULL which terminates the loop.

This is helpful when you don't want to read an extra line.

ok..thanks for clearing that up..

Your welcome. Mark this thread as solved then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.