1

What I am doing -

I am trying to read a file that contains information about processes and create 3 different arrays. One for array name, second for arrival time and third for process time and compute it afterwards. The number of processes are not fixed but for testing purposes I am keeping it to 4 processes.

The first printf line outputs what I desire. The file contents in the form of array.

void readFile() {

int i = 0;
char printLine[10];
char *processName[4];
char *arrivalTime[4];
char *processTime[4];

FILE *processFile = fopen("processes.txt", "r");

while(!feof(processFile)){ 

       fgets(printLine, 10, processFile); // get the line

       processName[i] = strtok(printLine, " ");
       arrivalTime[i] = strtok(NULL, " ");
       processTime[i] = strtok(NULL, "");
       printf("%s %s %s\n", processName[i], arrivalTime[i], processTime[i]);

       i++;       
}

printf("----\n%s %s %s\n", processName[0], arrivalTime[0], processTime[0]);
}

Error - The error(sort of) is that the output of 2nd print line gives me the last process information even though I am printing the 1st element(1st process) information. So, instead of printing 1st element it is printing the last.

processes.txt file looks like this

P1 0 3
P2 1 6
P3 4 4
P4 6 2

P.S. The format of this file will be fixed so no issue there.

I am a real real novice in this. Please excuse my silliness.

EDIT - my output

output image

5
  • Could you share your output? Commented Sep 19, 2017 at 4:19
  • You are saving the address of the same buffer part. You need to make a copy. Like processName[i] = strdup(strtok(printLine, " ")); Commented Sep 19, 2017 at 4:21
  • 1
    Also Read Why is “while ( !feof (file) )” always wrong? Commented Sep 19, 2017 at 4:22
  • edited. Included output msg Commented Sep 19, 2017 at 4:44
  • Thanks @BLUEPIXY it worked :D Commented Sep 19, 2017 at 4:50

1 Answer 1

1

This is because of the way strtok works, it doesn't return a pointer to a copy of the token, it returns a pointer to the first character of the token in the ORIGINAL location. You need to create a copy yourself.

basically, after the first read you have the following situation:


[ 1^'P','1','\0',2^'0','\0',3^'3','\0']

1: printLine,processName[0]
2: arrivalTime[0]
3: processTime[0]


  • So printLine and processName[0] are just pointing at the first position -marked by 1^-.

  • arrivalTime[0] is just pointing at the 4th position -marked by 2^-.

  • processTime[0] is just pointing at the 6th position -marked by 3^-.

Where basically all you "string"s -which are actually just character pointers- are pointing to different points within the same sequence of characters. When you read the new string, it overwrite the old data, but the pointers stay where they are.

After your loop is done you have the following situation:


[ 1^'P','4','\0',2^'6','\0',3^'2','\0']

1:printLine,processName[0],processName[1],processName[2],processName[3] 2:arrivalTime[0],arrivalTime[1],arrivalTime[2],arrivalTime[3] 3:processTime[0],processTime[1],processTime[2],processTime[3]


As you can see, everything is just pointing at different places in the same sequence of characters.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed answer. Got it. Solved :D
I'm glad it helped, can you please mark this answer as the solution (press the v button next to it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.