我不断收到一堆错误:
任何反馈意见都会被批准。感谢
#include <stdio.h>
#include <stdlib.h>
struct student{
char fname[21];
char lname[21];
float gpa;
} str;
int getStudentData(struct studnet *current_ptr); // struct function format
int main(void){
struct student str;
getStudentData(str);
printf("Last Name: %s\n First Name: %s\n GPA: %.2f\n", str.fname, str.lname, str.gpa);
return 0;
}
int getStudentData(struct studnet *current_ptr){
FILE *studentFile; // declaring a pointer file variable
studentFile = fopen("StudnetData.txt", "r"); // format for fopen; file-variable = fopen(file_name, mode);
if ( studentFile == NULL){
printf("Error: Unable to open StudnetData.txt file\n"); //test for error
}
else {
fscan(studentFile, "%20s %20s has a GPA of %f\n"
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
// fscanf(file, format, ¶meter-1, ...)
fclose(studentFile); // The function fclose will close the file.
}
return 0;
}
答案 0 :(得分:1)
int getStudentData(struct studnet *current_ptr)
^^^^^^^
您的意思是struct student
而不是struct studnet
。你不需要一个铆钉网,对吗?
getStudentData(str);
此外,这应该是
getStudentData(&str);
因为您正在修改传入的结构,为此,您需要一个指向它的指针(您已在函数原型中正确声明)。
此外,你还有另一个错字:
fscan(studentFile, "%20s %20s has a GPA of %f\n"
该函数的名称为fscanf()
,而非fscan()
。
我发现的最后一个错误:
fscanf(..., current_ptr->fname, current_ptr->lname, current_ptr->gpa)
^^^^^^^^^^^^^^^^
您没有将指针传递给gpa
成员,它必须是¤t_ptr->gpa
。
答案 1 :(得分:0)
我的编译器推出以下输出:
blah.c:10:27: warning: declaration of 'struct studnet' will not be visible outside of this function [-Wvisibility]
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:14:20: error: passing 'struct student' to parameter of incompatible type 'struct studnet *'
getStudentData(str);
^~~
blah.c:10:36: note: passing argument to parameter 'current_ptr' here
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:20:27: warning: declaration of 'struct studnet' will not be visible outside of this function [-Wvisibility]
int getStudentData(struct studnet *current_ptr){
^
blah.c:20:5: error: conflicting types for 'getStudentData'
int getStudentData(struct studnet *current_ptr){
^
blah.c:10:5: note: previous declaration is here
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:30:9: warning: implicit declaration of function 'fscan' is invalid in C99 [-Wimplicit-function-declaration]
fscan(studentFile, "%20s %20s has a GPA of %f\n"
^
blah.c:31:30: error: incomplete definition of type 'struct studnet'
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
~~~~~~~~~~~^
blah.c:20:27: note: forward declaration of 'struct studnet'
int getStudentData(struct studnet *current_ptr){
许多警告是因为您错误拼写student
为studnet
。例如,这一个:
blah.c:31:30: error: incomplete definition of type 'struct studnet'
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
~~~~~~~~~~~^
我们还应该通过值传递指针而不是stuct。将行更改为getStudentData(&str);
会有以下错误:
blah.c:14:20: error: passing 'struct student' to parameter of incompatible type 'struct studnet *'
getStudentData(str);
^~~
最后,我想你想要fscanf
而不是fscan
,这会解决这个错误:
blah.c:30:9: warning: implicit declaration of function 'fscan' is invalid in C99 [-Wimplicit-function-declaration]
fscan(studentFile, "%20s %20s has a GPA of %f\n"
^
此后续错误没有警告(但是,由于fscanf错误),但如果我修复了上述错误,我会再收到一条需要修复的警告。这可以通过传递¤t_ptr->gpa
而不是current_ptr->gpa
来完成。
blah.c:31:59: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
^~~~~~~~~~~~~~~~