Question
In C write a program that must accept one argument from the command line. The argument...
In C write a program that must accept one argument from the command line. The argument is the name of the file containing the processes (for example: processes.csv).This file is comma-separated with three columns (process ID, arrival time and burst time) with each row for an individual process. You can assume that this file will have a maximum of 10 processes.
For example
Input: processes.csv
ProcessID,Arrival Time,Burst Time
0,1,3
1,0,5
2,9,8
3,10,6
Where the first element is processes, the second is Arrival time and the third is Burst time. Store each in separate arrays (one for processes, one for arrival and one for burst)
Thanks!
Answers
The code in image:
The code in the text:
#include<stdio.h> //including pacakages
#include<string.h>
#include<ctype.h>
int main() {
FILE *fp1;
char file_name[100],c; //declaration of characters
printf("Enter name of the file: ");
scanf(" %s",file_name); //taking file name
fp1=fopen(file_name,"r"); //opening the file
int list[30],process_id[10],Arrival_time[10],Burst_time[10],len; //decalring list
c=fgetc(fp1); //taking character from fp1 pointer or file
int i=0,number,num=0;
while(c!='\n'){ //iterating until it hits a newline
c=fgetc(fp1);
}
c=fgetc(fp1);
while(c!=EOF){ //iterate until end of file
if (isdigit(c)){ //if it is digit
sscanf(&c,"%d",&number); //changing character to number (c)
num=(num*10)+number; //if it has more than 1 digit..ex:-12== 0*10+1=1 , 1*10+2=12
}
else if (c==',' || c=='\n') { //if it is new line or , then it will store the number in list
list[i]=num;
num=0;
i++;
}
c=fgetc(fp1);
}
int j=0,length=i+1;
for(i=0;i<length;i++){ //iterating through each numbre storing it in corresponding lists
process_id[j]=list[i]; //adding numbers into lists using indices
Arrival_time[j]=list[i+1];
Burst_time[j]=list[i+2];
i=i+2;
j++;
}
printf("process id,arrival id,burst time\n");
for(i=0;i<(length/3);i++){
printf("%d, ",process_id[i]); //printing every number
printf("%d, ",Arrival_time[i]);
printf("%d\n",Burst_time[i]);
}
return 0;
}Output:
1 #includec stdio.h> 2 #include<string.h> 3 #include«ctype.h> 4 int main) lincluding pacakages FILE *fp1; char file_name[100],c; //declaration of characters printf("Enter name of the file: "; scanf(" %s" ,filename); fp1-fopen(file_name,"r"); int list [30],process id[10],Arrival_time [10], Bursttime[10],len; //decalring list c-fgetc(fp1); //taking file name 10 //opening the file 12 13 14 15 16 17 18 19 20 21 //taking character from fp1 pointer or file int i 0,number,num o; //iterating until it hits a newline c-fgetc(fp1); c-fgetc(fp1); while(c !=EOF){ //iterate until end of file if (isdigit(c)) //if it is digit 23 24 25 26 27 28 29 30 31 32 sscanf(Ac,'%d" ,&number) ; num-(nun*10)+number //changing character to number (c) /if it has more than 1 digit..ex: -120 10+1-1 110+2-12 else if //if it is new line or, then it will store the number in list (c=-' ,' List[i]-num; num 0; ll c='\n') { c-fgetc(fp1); int j o,length-i+1; 34 35 36 37 38 39 40 for(i 0;i<length;i++) //iterating through each numbre storing it in corresponding lists process id[j1-list[i]; Arrival tine[j)-list[i+1] Burst time[j]-list[i+2]: i-i+2; //adding numbers into lists using indices 4243 printf("process id,arrival id,burst time\n") for(i:0; İs(length/3) ;i++){ 45 46 47 48 49 50 51 //printing every number printf("%d, -id[i]); printf("%d, "Arrival-time[i]); printf("%d\n" , Burst-time[i]); , process return อ;[email protected]:~$ ./a.out Enter nane of the file: processes.csv process id,arrival id,burst tine 0, 1, 3 11, θ, 5 2, 9, 8 3, 10, 6 5 11 ackaiack-Travel Mate