-1
此代码只读取文本文件中的第一行。我怎样才能读取同一文本文件中的多行?谢谢。
L-NAME FNAME具有88.0
L-NAME FNAME的GPA具有90.0如何读取同一文本文件中的多行?
#include <stdio.h>
#include <stdlib.h>
struct stud{
char fname[21];
char lname[21];
float gpa;
} str;
int getStudData(struct stud *current_ptr); // struct function format
int main(void){
struct stud str;
getStudData(&str);
printf("Last Name: %s\n First Name: %s\n GPA: %.2f\n"
, str.fname, str.lname, str.gpa);
return 0;
}
int getStudData(struct stud *current_ptr){
FILE *studFile; // declaring a pointer file variable
studFile = fopen("StudData.txt", "r"); // format for fopen; file-variable = fopen(file_name, mode);
if (studFile == NULL){
printf("Error: Unable to open StudData.txt file\n"); //test for error
}
else {
fscanf(studFile, "%20s %20s has a GPA of %f\n"
, current_ptr->fname, current_ptr->lname, ¤t_ptr->gpa);
// fscanf(file, format, ¶meter-1, ...)
fclose(studFile); // The function fclose will close the file.
}
return 0;
}
那么哪个while循环我应该在此代码中使用注释fclose后?谢谢。 – c0ldhand 2013-03-17 19:17:50
我会建议像这里发布的解决方案... [C++示例使用](http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line) –
jarrodparkes
2013-03-17 19:19:05
我很欣赏你的意见。我找到了解决方案。谢谢。 – c0ldhand 2013-03-17 19:48:58