I'm really new to C and I'm definitely open to guidance rather than the answer but i really just need to be pointed in the right direction more than anything so any help is appreciated. Also, I'm using functions supplied partially by my professor so if something is deprecated/not recommended that's why (so yes it is an assignment).
My function is supposed to read from a file that formats numbers like this
Code:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
and so on... and return it into an array.
Here is my array load:
Code:
int fillProcessesFromFile(struct processblock procs[], FILE* fPtr, int maxSize) {
int cnt = 0;
int pid;
int uid;
int aTime;
int pri;
int etr;
int emn;
int epc;
// loop until end of file or array full -
while ((cnt < maxSize) && fscanf(fPtr, "%d %d %d %d %d %d %d", &pid, &uid, &aTime, &pri, &etr, &emn, &epc) != EOF) {
procs[cnt].processId = pid;
procs[cnt].userId = uid;
procs[cnt].arrivalTime = aTime;
procs[cnt].priority = pri;
procs[cnt].expectedTimeRemaining = etr;
procs[cnt].expectedMemoryNeed = emn;
procs[cnt].expectedPctCPU = epc;
}
return cnt;
}
And here is where it's called:
Code:
int main() {
struct processblock processes[600];
FILE* filePtr;
errno_t err;
err = fopen_s(&filePtr, "processes.txt", "r"); // open file for reading
int numProcs = 1;
// make sure it opens
if (err != 0) {
perror("Error");
printf("Can't open the file processes.txt\n");
getchar();
return 1; // indicate unsuccessful
}
numProcs = fillProcessesFromFile(processes, filePtr, 600);
fclose(filePtr);
printf("%d", numProcs);
getchar();
and lastly here's my struct
Code:
struct processblock {
int processId;
int userId; // pointer instead?
int arrivalTime; // time units in simulation, not actual time
int priority; // base priority
int expectedTimeRemaining;
int expectedMemoryNeed;
int expectedPctCPU; // to get an idea of whether CPU bound or IO bound
};
In main(), numProcs is returning 0 so it's obvious the count is returning 0. CAn't figure out why. THe file must be opening otherwise err would have errored.