Ich schreibe ein c-Programm zur Simulation des FCFS-Scheduling-Algorithmus. Es akzeptiert ein Befehlszeilenargument als Datei und berechnet die Bearbeitungszeit und die Wartezeit für jeden Prozess. Es kann jedoch Werte aus einer Textdatei nicht erfolgreich in Variablen lesen. Hier ist der CodeC Werte aus Textdatei lesen
#include <stdio.h>
#define N 50
int main(int argc, char** argv)
{
int i = 0;
char line[20];
int n=0;
typedef struct
{
char name; //process name
int at; //arrive time
int pt; //process time
int ft; //finish time
int rt; //round time
int wt; //wait time
} Process;
Process pcs[N];
FILE* file = fopen(argv[1], "r");
while (fgets(line,sizeof(line),file) != NULL)
{
sscanf(line, "%s %d %d", pcs[i].name, pcs[i].at, pcs[i].pt);
line[strlen(line)-1] = '\0';
printf("%s %d %d\n",pcs[i].name, pcs[i].at, pcs[i].pt);
i++;
}
fclose(file);
pcs[0].ft=pcs[0].at+pcs[0].pt;
pcs[0].rt=pcs[0].ft-pcs[0].at;
pcs[0].wt=0;
for (n;n<4;n++)
{
if (pcs[n].at<pcs[n-1].ft)
{
pcs[n].ft=pcs[n-1].ft+pcs[n].pt;
pcs[n].rt=pcs[n].ft-pcs[n].at;
pcs[n].wt=pcs[n-1].ft-pcs[n].at;
}
else
{
pcs[n].ft=pcs[n].at+pcs[n].pt;
pcs[n].rt=pcs[n].ft-pcs[n].at;
pcs[n].wt=pcs[n-1].ft-pcs[n].at;
}
}
int x = 0;
for (x;x<n;x++)
{
printf("process name: %s", pcs[x].name);
printf("Turnaround Time: %d", pcs[x].rt);
printf("Wait Time: %d\n", pcs[x].wt);
}
return(0);
}
Hier ist die Eingabedatei
Und der Ausgang ist
Vielen Dank für jede Hilfe und Beratung.
) 'char name' speichert *** genau ein *** Charakter, dass es . b.) Nehmen Sie die Warnungen des Compilers ernst. – alk
Kompilieren mit allen Warnungen & Debug-Informationen ('gcc -Wall -Wextra -g') dann ** benutze den Debugger **' gdb' –
Danke für den Hinweis. Problem gelöst. – harry