为什么此代码在 C 中打开文件时出现问题?

问题描述 投票:0回答:1

我正在 MacOS 上编码。我在打开文件时遇到一些问题。

我不知道为什么文件没有被读取。文本文件位于同一目录中。但Unix可执行文件的结果是:

scheduling.txt
file open error: 2
Could not open data file: No such file or directory
zsh: abort      

enter image description here

如你所见,名字没有问题。

// 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("Input\n");
    printf("---------------------------------------------------------\n");
    printf("Process ID\t Arrival Time\t Burst Time\t Priority\n");
    printf("---------------------------------------------------------\n");
    for (int i = 0; i < numProcess; i++) {
        printf("%2d\t\t %2d\t\t %2d\t\t %2d\n", 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("%s\n", filename);

    // Open file function
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("file open error: %d\n", 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;
}

我认为我正确编写了代码,但文件未打开

c file fopen
1个回答
-1
投票

bruh 你在编写代码之前或之后保存了文件吗?

© www.soinside.com 2019 - 2024. All rights reserved.