I am working on a Priority Based Scheduler which reads the process in from a file. I have it reading from file and printing to the screen. I'm trying to use strtok to tokenize a line from the process test file and store it in three different arrays. The layout of the process file is:

Process Quanta Priority
Process0 12 1
Process1 10 9
Process2 15 4
And so on for about 800 lines.

I want it to token the spaces between the process and it's quanta and priority and store it in corresponding arrays.

So basicly:
Process0 will be stored in array Process, Quanta in Quanta, Priority in Priority

When it reads in the next line it repeats the storing steps by incrementing the i.

This is what I have so far.


char delims[] = " ","\n";
	char *result = NULL;
	result = strtok( line, delims );
	while( result != NULL ) 
	{
	    if(result = " ")
	    {
            }

	    fprintf( file1, "%s\n", result );
	    printf("Process Printed to file\n");
	    result = strtok( NULL, delims );
	}

(The Process Printed to File is for my own references to make sure strtok was working)

Any help would be much appreciated.

Recommended Answers

All 6 Replies

There are a few errors in your code. First, char delims[] = " ", "\n"; is invalid and your compiler should have told you so. You probably want something like char * delims = " \n"; Also, if (delims = " ") is an assignment statement and in your code the compiler should warn or error about this as well. You can not compare character arrays in C with the == operator, you must use strcmp or an equivalent mechanism. That aside, you really dont need to check (or set) the value of delims within the loop body as it is not modified by the call to strtok .

One other thing, be aware that strtok modifies the searched string. If you want to store these values for later use you will need to make a copy to permanent memory.

Thanks for the information I have made some changes since posting.

char delims[] = " ""\n";
	char *result = NULL;
	result = strtok( line, delims );
	while( result != NULL ) 
	{
	    if(result = " ")
	    {
		printf("Space Detected\n");
	    }    
	    else
	    {
		print("%s",result);
	    }
	   /* fprintf( file1, "%s\n", result );
              printf("Process Printed to file\n");*/
	    result = strtok( NULL, delims );
	}

Will this make it any easier to get to my goal if not what would you suggest I do.

Personally, I'd use fgets() to get the whole line of text from the process file:
fgets(charBufferName, sizeof(charBufferName), filePointerName);

which easily gets 1 line of data, and in a while loop, gets every line of data:

int i=0;
while((fgets(buffer, sizeof(buffer), fpName)) != NULL) {
   //then use sscanf() to get the data into your arrays:
   sscanf(buffer, "%s %s %s ", array1[i],array2[i],array3[i]);
   ++i;
}

strtok() is fine, but you always have to watch out for what you can't see - that it changes the string it's tokenizing.

And here's how you use strtok (you know... for future references).

#include <stdio.h>
#include <string.h>
int main(void){
    char line[20] = "Process0 12 1"; // suppose this is the first line read by fgets
    char* delim=" ";
    char* result = NULL;
    result=strtok(line,delim);
    if(result != NULL)
        printf("process:%s\n",result);//store process in an array
    result=strtok(NULL,delim);//first argument NULL except first call to strtok
    printf("quanta:%s\n",result);//store quanta in another array,you can use atoi to change
                                 //quanta to integer type

    result=strtok(NULL,delim);//first argument NULL except first call to strtok
    printf("priority:%s\n",result);//store priority in another array
return 0;
}

Just making sure as the data i want to store in the separate arrays are on the same line, will the fgets also work for this??

And the code;

if(result != NULL)
        printf("process:%s\n",result);//store process in an array
    result=strtok(NULL,delim);//first argument NULL except first call to strtok
    printf("quanta:%s\n",result);//store quanta in another array,you can use atoi to change
                                 //quanta to integer type
 
    result=strtok(NULL,delim);//first argument NULL except first call to strtok
    printf("priority:%s\n",result);//store priority in another array

Will it work by putting it in an if loop and using an increment i??

I have got the strtok working, is there a way to access the arrays that they have been stored to so that I am able to sort them out in order of priority.

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.