gpt4 book ai didi

c - 我不知道我的代码是否正确。我读过 while (! feof (arch)) 总是错误的

转载 作者:行者123 更新时间:2023-11-30 14:55:08 27 4
gpt4 key购买 nike

这个想法是为学生的成绩创建一个数据库,但我不知道这段代码有什么问题

char bas;
char cadena[100];
FILE *arch, *pagC;
int c;
arch=fopen("alumnos.txt","r");
pagC=fopen("calificaciones.html","w");
/*html tags for a table*/
while(!feof(arch))
{
fprintf(pagC,"<TR>");
while(c!='\n')
{
fscanf(arch,"%[^:]%c",cadena,&bas);
fprintf(pagC,"<TD>%s</TD>",cadena);
}
fprintf(pagC,"</TR> \n");
}
fprintf(pagC,"</TABLE> </CENTER> </BODY> </HTML>");
fclose(pagC);
fclose(arch);

最佳答案

这意味着:

while(getchar()!='\n')

您正在逐个字符地读取文件,但您对换行符不感兴趣\n

<小时/>

顺便注意@WhozCraig 的这条重要评论

Equally important, A newline will terminate the loop, but no matter what was read, it is lost. Loop continuation only means whatever it was, it wasn't a newline. Further, getchar() is likely reading from stdin, while the statements inside appear only interested in file IO. Needless to say, this loop is flat-out-odd.

关于c - 我不知道我的代码是否正确。我读过 while (! feof (arch)) 总是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com