从文件读取和打印但卡在循环 C
Reading and Printing from a file but stuck in loop C
我试图从文件中读取一些数据然后将其打印出来,但我的代码只读取了第一个内容,然后陷入无限循环(在 while 循环中)。我究竟做错了什么?
我的输出只是学生:艾比 GPA:3
我正在使用 Visual Studio 2012。
我只是按照我书中的一个例子。
//My data is Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
unsigned int GPA;//GPA
char student[10];//Student
FILE * cfPter;
//Check if file opened
if((cfPter = fopen("data.txt", "r")) ==NULL)
{
puts("File could not be opened");
}
//Read Contents
else
{
puts("Contents of file:\n");
fscanf(cfPter,"%s %f ", student, &GPA);
}
//While not at end Print the contents read
while(!feof(cfPter))
{
printf("Student: %s GPA: %f",student,GPA);
fscanf(cfPter, "%s %f", student, GPA);
//system("pause");
}
fclose(cfPter);
system("pause");
} //end main
您正在实现目标,但进行一些调整可以让生活更轻松。首先,如果您的 fopen
失败,要么通过提示输入另一个文件名来处理失败,或者在此时简单地 return
(exit
)。这样,您的其余代码就不会包含在 else
语句中。
接下来,我提供了 link 为什么 while (!feof(file))
总是错误的(当从文件中读取字符数据时)。当您读取输入时,验证您收到了输入——这就是您真正需要做的全部。检查 return 您的 fscanf
电话。
考虑到这一点,您可以执行类似以下操作:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
float GPA = 0.0; /* GPA */
char student[10] = ""; /* Student */
FILE *cfPter = NULL;
/* open file/validate file is open */
if (!(cfPter = fopen ("data.txt", "r"))) {
fprintf (stderr, "error: file open failed 'data.txt'.\n");
return 1;
}
/* Read Contents */
while (fscanf (cfPter, " %9s %f", student, &GPA) == 2)
printf ("Student: %-10s GPA: %.2f\n", student, GPA);
fclose (cfPter);
return 0; /* main is type 'int' and returns a value */
}
例子data.txt
$ cat data.txt
Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
例子Use/Output
$ ./bin/feofissue
Student: Abbie GPA: 3.40
Student: Oakley GPA: 3.50
Student: Sylvia GPA: 3.60
Student: Uwe GPA: 3.70
Student: Ken GPA: 3.80
Student: Aaron GPA: 3.90
Student: Fabien GPA: 4.00
(注意 虽然 MS 很早就让你使用 void main
,但 main
被定义为类型 int
和 return是一个值。)
还有pause
,你通常在windows上#include <conio.h>
并调用getch();
来防止终端window关闭。您可以尝试任何一种方式。如果您有任何问题,请告诉我。
我也在研究 this,有人告诉我尝试使用 strcmp() 逐行读取文件,直到找到您要查找的行?我正在研究这个想法,但还没有弄清楚在那之后它如何能够读取 GPA。
避免在 fscanf 中读取不同类型的日期的一个计划是始终将 char 数据读入本地 char 数组。然后使用 sscanf 将其转换为您需要的数据类型。这允许您在 fscanf 和 sscanf 之间添加数据检查。这将避免 fscanf 什么都不读(纺车)并且永远不会到达 eof
我试图从文件中读取一些数据然后将其打印出来,但我的代码只读取了第一个内容,然后陷入无限循环(在 while 循环中)。我究竟做错了什么? 我的输出只是学生:艾比 GPA:3 我正在使用 Visual Studio 2012。 我只是按照我书中的一个例子。
//My data is Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
unsigned int GPA;//GPA
char student[10];//Student
FILE * cfPter;
//Check if file opened
if((cfPter = fopen("data.txt", "r")) ==NULL)
{
puts("File could not be opened");
}
//Read Contents
else
{
puts("Contents of file:\n");
fscanf(cfPter,"%s %f ", student, &GPA);
}
//While not at end Print the contents read
while(!feof(cfPter))
{
printf("Student: %s GPA: %f",student,GPA);
fscanf(cfPter, "%s %f", student, GPA);
//system("pause");
}
fclose(cfPter);
system("pause");
} //end main
您正在实现目标,但进行一些调整可以让生活更轻松。首先,如果您的 fopen
失败,要么通过提示输入另一个文件名来处理失败,或者在此时简单地 return
(exit
)。这样,您的其余代码就不会包含在 else
语句中。
接下来,我提供了 link 为什么 while (!feof(file))
总是错误的(当从文件中读取字符数据时)。当您读取输入时,验证您收到了输入——这就是您真正需要做的全部。检查 return 您的 fscanf
电话。
考虑到这一点,您可以执行类似以下操作:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
float GPA = 0.0; /* GPA */
char student[10] = ""; /* Student */
FILE *cfPter = NULL;
/* open file/validate file is open */
if (!(cfPter = fopen ("data.txt", "r"))) {
fprintf (stderr, "error: file open failed 'data.txt'.\n");
return 1;
}
/* Read Contents */
while (fscanf (cfPter, " %9s %f", student, &GPA) == 2)
printf ("Student: %-10s GPA: %.2f\n", student, GPA);
fclose (cfPter);
return 0; /* main is type 'int' and returns a value */
}
例子data.txt
$ cat data.txt
Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
例子Use/Output
$ ./bin/feofissue
Student: Abbie GPA: 3.40
Student: Oakley GPA: 3.50
Student: Sylvia GPA: 3.60
Student: Uwe GPA: 3.70
Student: Ken GPA: 3.80
Student: Aaron GPA: 3.90
Student: Fabien GPA: 4.00
(注意 虽然 MS 很早就让你使用 void main
,但 main
被定义为类型 int
和 return是一个值。)
还有pause
,你通常在windows上#include <conio.h>
并调用getch();
来防止终端window关闭。您可以尝试任何一种方式。如果您有任何问题,请告诉我。
我也在研究 this,有人告诉我尝试使用 strcmp() 逐行读取文件,直到找到您要查找的行?我正在研究这个想法,但还没有弄清楚在那之后它如何能够读取 GPA。
避免在 fscanf 中读取不同类型的日期的一个计划是始终将 char 数据读入本地 char 数组。然后使用 sscanf 将其转换为您需要的数据类型。这允许您在 fscanf 和 sscanf 之间添加数据检查。这将避免 fscanf 什么都不读(纺车)并且永远不会到达 eof