Hi,

I'm new to this site so apologies for any mistakes. I'm trying to write a program to parse string from a file. In each string I then have to parse the different strings for example:

add h'13', 4

So I've to parse the 'add' bit, the '13' bit and the '4' bit. At the minute I'm trying to use strtok but I'm getting a run time error.

/* Libraries */
#include <stdio.h>
#include <string.h>

/* Main Function */
int main()
{
	/* Declarations */
    char *instruction;
    char *action;
    char *number;
    char *number2;
    
    instruction = "add  h'13', 4";
    
    printf("%s \n",instruction);

// The run time error occurs here

    action = strtok ( instruction, " " );
    number = strtok ( NULL, " ' " );
number2 = strtok ( NULL, "," );

	printf("%s \n %s \n %s \n", action, number, number2);

	system("PAUSE");
	return 0;
}

Any help you can give me would be greatly appreciated. Thanks.

Try defining your string "instruction" like below...
Also if you have errors in the future could you post them

/* Libraries */
#include <stdio.h>
#include <string.h>

char instruction[] = "add  h'13', 4";//not a constant now

/* Main Function */
int main()
{
	/* Declarations */
    
    char *action;
    char *number;
    char *number2;
    
    printf("%s \n",instruction);

// The run time error occurs here

    action = strtok ( instruction, " " );
   number = strtok ( NULL, " ' " );
number2 = strtok ( NULL, "," );

	printf("%s \n %s \n %s \n", action, number, number2);

	system("PAUSE");
	return 0;
}

Thanks very much that helped a lot :)

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.