I have a C program where I’m reading in from a file and then trying to print it to test it at the moment. The thing i’m having issues is that my const in MAX = 40 and its printing that amount of times. I’ve tried passing a reference to jobcount in the function but I get an error when I add * after int and an & before jobcount when passing it. I appreciate the help, its always quality input on all issues!
#include <stdio.h>
struct record{
char name[1];
int arrival_time;
int job_length;
int job_priority;
};
const int MAX = 40;
void fileinput(struct record jobs[MAX], int jobcount);
void output(struct record jobs[MAX], int jobcount);
int main(void)
{
struct record jobs[MAX];
int jobcount;
fileinput(jobs,jobcount);
output(jobs,jobcount);
return(0);
}
void fileinput(struct record jobs[MAX], int jobcount){
jobcount = 0;
FILE *f = fopen("data.dat","r");
while(fscanf(f, "%s %d %d %d", jobs[jobcount].name, &jobs[jobcount].arrival_time, &jobs[jobcount].job_length, &jobs[jobcount].job_priority) != EOF)
{
jobcount++;
printf("READ IN TEST \n");
}
}
void output(struct record jobs[MAX], int jobcount){
int j = 0;
for(j = 0;j < jobcount; j++)
{
printf("%s %d %d %d\n", jobs[j].name, jobs[j].arrival_time, jobs[j].job_length, jobs[j].job_priority);
printf("FOR LOOP TEST \n");
}
}
data.dat looks like this
A1 3 3 3
B1 4 4 4
C1 5 5 5
You should not get an error. Probably you might have changed in the forward declaration alone and forgot to change at the definition.
And definition –
Since
jobcountis a pointer, you need to dereference first to modify/access the value it is pointing at. You need to post the exact error message for further help though.