Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将feof与while循环一起使用?_C_File_Loops_For Loop_While Loop - Fatal编程技术网

C 如何将feof与while循环一起使用?

C 如何将feof与while循环一起使用?,c,file,loops,for-loop,while-loop,C,File,Loops,For Loop,While Loop,我想得到学生的期中和期末分数,并将它们写在一个txt文件中,但当我使用循环时,它永远不会得到学生的名字。它总是给它一个错过。如何将feof与循环一起使用?我想得到期中和期末的学生姓名,并根据得到的分数计算平均值,它必须始终得到姓名和分数,直到用户按下文件末尾 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<conio.h> void main() {

我想得到学生的期中和期末分数,并将它们写在一个txt文件中,但当我使用循环时,它永远不会得到学生的名字。它总是给它一个错过。如何将feof与循环一起使用?我想得到期中和期末的学生姓名,并根据得到的分数计算平均值,它必须始终得到姓名和分数,直到用户按下文件末尾

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<conio.h>

void main()
{
FILE *Points;

char namesOfStudents[10];
int pointsOfStudents[1][2];
double AverageOfStudents[1];
int i=0,j=1;
int numberOfStudents;

Points = fopen("C:\\Users\\Toshiba\\Desktop\\PointsOfStudent.txt", "a+");
fprintf(Points, "Name\t\t 1.Grade\t2.Grade\t\tAverage\n");
/*  printf("How many students will you enter: ");
scanf("%d",&numberOfStudents);*/

//while (!feof(Points))

printf("Please enter new students name: ");
gets(namesOfStudents);
printf("\nPlease enter new students first point: ");
scanf("%d",&pointsOfStudents[0][0]);
printf("\nPlease enter new students second point: ");
scanf("%d",&pointsOfStudents[0][1]);


        for (; i < strlen(namesOfStudents); i++)
            {
                fprintf(Points, "%c", namesOfStudents[i]); //To write 
     student name to file

            }
        fprintf(Points,"\t\t   ");

        fprintf(Points,"%d\t\t",pointsOfStudents[0][0]);  //to write 
student's first point
        fprintf(Points,"%d\t\t",pointsOfStudents[0][1]);  //to write 
student's second point  

        fprintf(Points,"%d\n",(pointsOfStudents[0][0]+pointsOfStudents[0]
[1])/2);    //to calculate and write average 
        system("cls");

        fclose(Points);

system("Pause");
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
void main()
{
文件*点;
学生的姓名[10];
学生积分[1][2];
学生平均数的两倍[1];
int i=0,j=1;
国际学生人数;
Points=fopen(“C:\\Users\\Toshiba\\Desktop\\pointsofsofstudent.txt”,“a+”;
fprintf(点,“名称\t\t 1.Grade\t2.Grade\t\t平均值\n”);
/*printf(“您将输入多少名学生:”);
scanf(“%d”和numberofstudent)*/
//而(!feof(点))
printf(“请输入新生姓名:”);
获取(学生的名称);
printf(“\n请输入新生第一点:”);
scanf(“%d”、&pointsofsofstudent[0][0]);
printf(“\n请输入新生第二点:”);
scanf(“%d”、&pointsofsofstudent[0][1]);
对于(;i
有几件事:

首先,决不使用
获取
-这很危险,它会在代码中引入故障点和/或巨大的安全漏洞,并且从2011版语言标准开始,它已从标准库中删除。改用
fgets

fgets( nameOfStudents, sizeof nameOfStudents, stdin );
其次,
而(!feof(fp))
总是错误的。当从
fp
输入时,它将太频繁地循环一次。在输出到
fp
时,它没有意义

您可以使用
fgets
的结果来控制循环:

while ( fgets( nameOfStudents, sizeof nameOfStudents, stdin ) )
{
  ...
}
从终端输入数据后,使用CtrlZ或CtrlD(取决于您的平台)发出EOF信号

第三,
main
返回
int
,而不是
void
;使用

int main( void )
相反

最后,改变

for (; i < strlen(namesOfStudents); i++)
{
  fprintf(Points, "%c", namesOfStudents[i]); //To write student name to file
}
将学生姓名写入文件

还有其他问题,但是做些改变,看看这是否有帮助

fprintf( Points, "%s", nameOfStudents );