- #1
- 108
- 0
What I want to do is to to read the number of processes, their arrival time, and then in the next line, their bursts. Here's the file format:
P1 0
12 2 21 2 12 32 18
P2 9
13 17 3 21 45 67 21 2
(1st line) Process Arrivaltime
(2nd line) Bursts
(3rd line) Process Arrivaltime
(4th line) Bursts
and so on.
What I've done so far is to count the number of processes, and pick up their arrival times as integers. I've also calculated the number of bursts for each processes. For example, in the above two processes, the bursts for P1 are 7 and for P2 bursts are 8 (so far so, good). Now I want to read their bursts from the next line in another int array. The code so far is:
Now, I can't figure how to get the bursts in this array. I've tried a couple of things but they're not working good. What should I do?
P1 0
12 2 21 2 12 32 18
P2 9
13 17 3 21 45 67 21 2
(1st line) Process Arrivaltime
(2nd line) Bursts
(3rd line) Process Arrivaltime
(4th line) Bursts
and so on.
What I've done so far is to count the number of processes, and pick up their arrival times as integers. I've also calculated the number of bursts for each processes. For example, in the above two processes, the bursts for P1 are 7 and for P2 bursts are 8 (so far so, good). Now I want to read their bursts from the next line in another int array. The code so far is:
Code:
#include<stdio.h>
#include <malloc.h>
struct os
{
char process ;
int arrival_time;
int *burst;
int l;
};
int main()
{
char a;
int b=0;
struct os fast[6];
FILE *far;
FILE *f;
int m;
char s[1000];
int pro=0;
far=fopen("data.txt","r");
/////////////////////////////This will read the entire file and see the number of processes///////////////////////////////
while (fgets(s,1000,f)!=NULL)
{
if (s[m] == 'P')
pro++;
}
printf ("Process are: %d ",pro);
fclose(f);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////// Open the file again and get the process and respective arrival time ////////////////////
far=fopen ("data.txt","r");
int i,j;
for(i=0;i<pro;i++)
{
fast[i].l=0;
fscanf(far,"%c%d",&fast[i].process,&fast[i].arrival_time);
//printf("%c ",fast[i].process);
//fast[i].burst=malloc(fast[i].l*sizeof(int));
fscanf(far,"%c",&a);}
//////////////////////////////// This would calculate the number of bursts in the line following each process/////////////
while (a!='\n')
{
fscanf(far,"%c",&a);
}
fscanf(far,"%c",&a);
while (a!='\n')
{
if (a>='0')
{
b++;
}
else if (a=' ')
{
b=0;
}
if (a!=' '&& b==1)
{
fast[i].l++;
}
fscanf(far,"%c",&a);
}
printf("%d ",fast[i].l);
fast[i].burst=malloc(fast[i].l*sizeof(int)); // dynamically allocate the int bursts array for each process
}
fclose(far);
}
Now, I can't figure how to get the bursts in this array. I've tried a couple of things but they're not working good. What should I do?