我在桌面上用记事本放了一个txt文件,文件里面是“301101 张文 91
301102 陈辉 85
301103 王伟 76
301104 政委 69
301105 郭文涛 55”这个内容,写了这个程序向读取里面的数据,但是每次都显示打开文件失败,请问这是怎么回事
#include
#include
int main()
{
FILE* fp;
long num;
char stname[20];
int i, score;
int average_score = 0;
if ((fp = fopen("abc.txt", "r")) == NULL)
{
printf("File open error!");
exit(0);
}
for (i = 0; i < 5; i++)
{
fscanf(fp, "%ld%s%d", &num, stname, &average_score);
average_score += score;
printf("%ld%s%d\n", num, stname, score);
}
printf("Average score:%d\n", average_score/5);
if (fclose(fp)) {
printf("can not close the file!\n");
exit(0);
}
return 0;
}
这个文件要么放在和这个程序同一个文件夹下,要么使用路名,不能光使用文件名,程序找不到文件就会打开失败
你这样写abc.txt,就是表明这个txt跟你的exe在一起放着
要么把你的exe拿到桌面上来,要么把txt放到exe文件夹里去
要么把路径补齐