当我去编写我的实验室时,我得到了一个错误。如果有人能告诉我我做错了什么会很棒,因为现在我被卡住了。错误似乎与我的fopen声明有关,但我们的老师并没有真正教过我们,只是告诉我们使用它。非常感谢提前
lab1.c: In function ‘read_file’:
lab1.c:65: warning: passing argument 1 of ‘fopen’ from incompatible pointer type
/usr/include/stdio.h:271: note: expected ‘const char * __restrict__’ but argument is of
type 'struct FILE *'
这是我的代码:
//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 to 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 to Open.");
exit(EXIT_FAILURE);
}
int num_students;
fscanf(input, "%d", &num_students);
for(i = 0; i < 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:");
for (i = 0; i < num_students; i++){
for(j = 0; j < class[i].num_A; j++){
printf("-");
}}
printf("\nB:");
for(i = 0; i < num_students; i++){
for(j = 0; j < class[i].num_B; j++){
printf("-");
printf("\nC:");
for(i = 0; i < num_students; i++){
for(j = 0; j < class[i].num_C; j++){
printf("-");
}}
printf("\nD:");
for(i = 0; i < num_students; i++){
for(j = 0; j < class[i].num_D; j++){
printf("-");
}}
printf("\nF:");
for(i = 0; i < 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; i < index1; i++){
if (class[i].num_A != 0){
gpa = class[i].num_A * 4;
As = 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( i = 0; i <= num_students; i++)
fprintf(output, "%c %d\n", &class[i].name, &class[i].gpa);
fclose(output);
}
答案 0 :(得分:3)
infile
应该是一个包含文件位置字符串的char *。不是FILE*
。
请参阅fopen的文档。
答案 1 :(得分:0)
你使用了错误的参数fopen()! fopen()返回一个FILE *,并获取一个const char *作为第一个参数。 那个男人可以帮到你:
NAME
fopen, fdopen, freopen - stream open functions
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fd, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
fdopen(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE