I’m coding on vscode on macos macbook. And I have some problems with opening the file.
I have no idea why file is not read. The text file is in same directory. but the result of Unix executable file is:
scheduling.txt
file open error: 2
Could not open data file: No such file or directory
zsh: abort
File directory
As you can see, the name is no problem.
// Priority.h
#ifndef DATA_H_
#define DATA_H_
// Process Structure
typedef struct {
int processID;
int arrivalTime;
int burstTime;
int priority;
}Process;
// Result Structure
typedef struct {
int processID;
int burstTime;
int waitingTime;
} ResultElement;
int OpenFile(const char* filename, Process *process[]); // File open
Process Initialize (Process* process, int i); // Intialize Queue
Process* ReadyQueueGenerator (int); // Generate Ready Queue
void ShowResult (ResultElement process); // Show Reulst
void SchedulingAlgorithm (Process process[], int); // Run Scheduling Algorithm
void Sort(Process process[], int); // Sort Process
#endif
// main.c
// Priority Scheduling Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Priority.h"
// Sort by Arrival Time
void Sort(Process process[], int numProcess) {
Process temp;
for (int i = 0; i < numProcess; i++) {
for (int j = i+1; j < numProcess; j++) {
if (process[i].arrivalTime > process[j].arrivalTime) {
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
}
int main() {
Process *process = NULL;
int numProcess = OpenFile("scheduling.txt", &process);
if (numProcess == -1) {
printf("Failed to open file.n");
return EXIT_FAILURE;
}
// List of process
printf("Inputn");
printf("---------------------------------------------------------n");
printf("Process IDt Arrival Timet Burst Timet Priorityn");
printf("---------------------------------------------------------n");
for (int i = 0; i < numProcess; i++) {
printf("%2dtt %2dtt %2dtt %2dn", process[i].processID, process[i].arrivalTime, process[i].burstTime, process[i].priority);
}
// Sort by Arrival Time
Sort(process, numProcess);
// Run Scheduling Algorithm
SchedulingAlgorithm(process, numProcess);
free(process);
return 0;
}
// FileOpen.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "Priority.h"
// Open file function
int OpenFile(const char* filename, Process **process) {
FILE *file;
int line = 0;
int i = 0;
printf("%sn", filename);
// Open file function
file = fopen(filename, "r");
if (file == NULL) {
printf("file open error: %dn", errno);
perror("Could not open data file");
abort();
}
char bin[20] = {0, };
while (fgets(bin, sizeof(bin), file) != NULL) {
line++;
}
fseek(file, 0, SEEK_SET);
*process = (Process*)malloc(sizeof(Process) * line);
if (process == NULL) {
fprintf(stderr, "Memory Allocation failure.n");
return -1;
}
while (fscanf(file, "%s %d %d %d %d", bin, &(*process)[i].processID, &(*process)[i].arrivalTime, &(*process)[i].burstTime, &(*process)[i].priority) != EOF) {
i++;
}
fclose(file);
return line;
}
I think I wrote codes properly but the file is not opened
New contributor
권기범 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.