c - I get an error with fopen -
when go , compile lab, 1 error. if tell me doing wrong awesome, because right stuck. error seems fopen statement, our teacher hasn't taught really, told use it. lot in advance
lab1.c: in function ‘read_file’: lab1.c:65: warning: passing argument 1 of ‘fopen’ incompatible pointer type /usr/include/stdio.h:271: note: expected ‘const char * __restrict__’ argument of type 'struct file *'
here code:
//libraries #include<stdio.h> #include<stdlib.h> #include<string.h> #define max_name_length 20 #define max_array_length 25 //structure typedef struct student_{ char name[max_name_length]; int num_a; int num_b; int num_c; int num_d; int num_f; float gpa; }student; student class[max_array_length]; //prototypes int read_file(file *input); void print_grade_distribution(int num_students); void calculate_gpa(int *index); void write_to_file(file *output, int num_students); int main(int argc, char* argv[]){ if(argc != 2) { printf("insufficient arguments"); exit(exit_failure); } if(argv[1] == null) { printf("failed print."); exit(exit_failure); } file *infile; file *outfile; int num_students = read_file(infile); print_grade_distribution(num_students); write_to_file(outfile, num_students); } int read_file(file* input){ int i; file *infile; input = fopen(infile, "r"); if(input == null) { printf("file failed open."); exit(exit_failure); } int num_students; fscanf(input, "%d", &num_students); for(i = 0; < num_students; i++){ fscanf(input, "%c %d %d %d %d", &class[i].name, &class[i].num_a, &class[i].num_b, &class[i].num_c, &class[i].num_d, &class[i].num_f); } int *index; calculate_gpa(index); fclose(input); return num_students; } void print_grade_distribution(int num_students){ int i; int j; printf("\na:"); (i = 0; < num_students; i++){ for(j = 0; j < class[i].num_a; j++){ printf("-"); }} printf("\nb:"); for(i = 0; < num_students; i++){ for(j = 0; j < class[i].num_b; j++){ printf("-"); printf("\nc:"); for(i = 0; < num_students; i++){ for(j = 0; j < class[i].num_c; j++){ printf("-"); }} printf("\nd:"); for(i = 0; < num_students; i++){ for(j = 0; j < class[i].num_d; j++){ printf("-"); }} printf("\nf:"); for(i = 0; < num_students; i++){ for(j = 0; j < class[i].num_f; j++){ printf("-"); }} } void calculate_gpa(int *index){ int i; int gpa; int as, bs, cs, ds, fs; int index1 = *index; for(i = 0; < index1; i++){ if (class[i].num_a != 0){ gpa = class[i].num_a * 4; = class[i].num_a; } if (class[i].num_b != 0){ gpa = gpa + (class[i].num_b *3); bs = class[i].num_b; } if (class[i].num_c != 0){ gpa = gpa + (class[i].num_c *2); cs = class[i].num_c; } if (class[i].num_d != 0){ gpa = gpa + (class[i].num_d *1); ds = class[i].num_d; } if (class[i].num_f != 0){ gpa = gpa + (class[i].num_f * 0); fs = class[i].num_f; } gpa = gpa / (as + bs + cs + ds + fs); gpa = class[i].gpa; } } void write_to_file(file *output, int num_students){ int i; for( = 0; <= num_students; i++) fprintf(output, "%c %d\n", &class[i].name, &class[i].gpa); fclose(output); }
infile
should char* containing string file location. not file*
.
see documentation fopen.
Comments
Post a Comment