0

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 

3 Answers 3

1

You should not get an error. Probably you might have changed in the forward declaration alone and forgot to change at the definition.

void fileinput(struct record jobs[MAX], int* jobcount); fileinput(jobs,&jobcount); 

And definition -

void fileinput(struct record jobs[MAX], int* jobcount) { // .... } 

Since jobcount is 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.

Sign up to request clarification or add additional context in comments.

6 Comments

This is the error I get. gcc projectv2.c -o project projectv2.c: In function ‘fileinput’: projectv2.c:26:38: error: array subscript is not an integer projectv2.c:26:60: error: array subscript is not an integer projectv2.c:26:90: error: array subscript is not an integer projectv2.c:26:118: error: array subscript is not an integer
@Intelwalk - As I mentioned earlier, you need to dereference the pointer. Array index must be an integer but what you are supplying is a pointer. So, you need to dereference jobcount before accessing like - (*jobcount) where ever you are using.
I get a segmentation fault now. Here is my code : #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"); } }
Post your snippet at www.pastebin.com. Learn to use debugger to find where exactly it is failing.
|
1

You would probably do best to revise fileinput() to return the job count:

int fileinput(struct record jobs[MAX]) { int jobcount = 0; ... return jobcount; } 

And then, in main():

int main(void) { struct record jobs[MAX]; int jobcount = fileinput(jobs); ... return 0; } 

The main alternative is to pass a pointer to jobcount to the fileinput() function. This would perhaps be better if you also returned a status (all OK, too many entries in the data, bogus data, etc). Your function would still return a value, but it would be the error indication, and the job count would be returned via the pointer.

A third (gruesome and not recommended) alternative is to use a global variable (or file static variable) for jobcount.

For a generalized input function, you should pass the size of the array explicitly, rather than assuming it is MAX:

int fileinput(struct record jobs[], size_t maxjobs); int jobcount = fileinput(jobs, MAX); 

You'd write the code to ensure that you do not exceed the specified number of job records.

Comments

1

You CAN get "jobcount" from fileinput() withing a "return", if you want to. You just need "&", passing in a pointer to "jobcount":

... const int MAX = 40; void fileinput(struct record jobs[MAX], int * jobcount); ... int main(void) { struct record jobs[MAX]; int jobcount=-1; 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 < MAX) ) { *jobcount++; printf("READ IN TEST \n"); } ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.