I am trying to read in a text file simulating CPU scheduling, perform the scheduling algorithm, and printing out the output.
i.e. process id number, arrival time, burst time
1 0 3
2 4 6
...
How can I assign the size of the struct to be created with the amount of lines scanned? Can I define MAX as the number of lines scanned? I am able to scan the input file and output the file, however, I have my MAX variable for the struct defined as 10000. The problem with this is that it prints to the file the correct output, but prints 0 0 0 up to 10000 lines after the input lines stop. Here's my functions.
#include<stdio.h>
#include<stdlib.h>
#define MAX 10000
typedef struct
{
int pid;
int arrTime;
int burTime;
int finTime;
int waitTime;
int turnTime;
}Process;
Process pTable[MAX];
void readTable(char *fileName, Process pTable[MAX])
{
int i;
FILE *fileIN = fopen(fileName, "r+");
while(!feof(fileIN))
{
fscanf(fileIN, "%d %d %d", &pTable[i].pid, &pTable[i].arrTime, &pTable[i].burTime);
i++;
}
fclose(fileIN);
}
void printTable(char *fileName, Process pTable[MAX])
{
FILE *fileOUT = fopen(fileName,"w+");
for(int i=0; i < MAX;i++)
{
fprintf(fileOUT, "%d %d %d\n",pTable[i].pid, pTable[i].arrTime, pTable[i].burTime);
}
}
int main(int argc, char **argv)
{
readTable(argv[1], pTable);
printTable(argv[2], pTable);
}
Here's the output I'm given for the shortened input file.
1 0 3
2 4 6
3 9 3
4 12 8
5 13 11
6 18 19
7 19 2
8 23 4
9 28 1
10 31 3
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0