【问题标题】:I get an error with fopen [closed]我收到 fopen 错误 [关闭]
【发布时间】:2014-07-22 15:16:02
【问题描述】:

当我去编译我的实验室时,我得到了这个错误。如果有人能告诉我我做错了什么,那就太棒了,因为现在我被困住了。错误似乎与我的 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);
}

【问题讨论】:

  • 错误告诉您问题所在 - 它需要一个文件名并返回 FILE*
  • fopen() 需要一个文件路径,你给它一个未初始化的文件*。
  • 有趣的是 C 只给出警告。
  • 好的错误信息很重要(它们属于标准错误):fp = fopen( path, mode ); if( fp == NULL ) { perror( path ); ...}
  • 所以把 fopen(infile, "r") 改成 fopen("input.txt", "r")

标签: c arrays error-handling compiler-errors


【解决方案1】:

您使用 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

【讨论】:

    【解决方案2】:

    infile 应该是一个 char*,其中包含一个带有文件位置的字符串。不是FILE*

    请参阅fopen 的文档。

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多