W_Reborn 2020-12-28 18:28 采纳率: 100%
浏览 136
已采纳

c语言程序无法读取文件

在Dev c++ 中,运行此程序结果是“File open error”

已经试过更换换文件类型(读取其他.c文件)、更换路径这些方法,还是file open error

感谢好哥哥解答

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
	FILE *fp;
	long num;
	char stname[20];
	int i, score;
	int avg_score = 0;
	
	if ((fp=fopen("d:\\C_works\\f12-1.txt", "r")) == NULL) {
		printf("File open error\n");
		exit(0);
	}
	for (i=0; i<5; i++) {
		fscanf(fp, "%ld%s%d", &num, stname, &score);
		avg_score += score;
		printf("%ld%s%d", num, stname, score);
	}
	
	printf("Average score: %d\n", avg_score/5);
	if (fclose(fp)) {
		printf("Can not close the file!\n");
		exit(0);
	}
	return 0;
}
  •  

  • 写回答

1条回答 默认 最新

  • SoftwareTeacher 《编程之美》作者 2020-12-29 03:17
    关注

    你用的什么操作系统?

    打开命令行 cmd 程序

    到 d:\C_works  目录下,

    输入 dir  命令,看看 你的 f12-1.txt  文件到底叫什么, 是不是叫  f12-1.txt.txt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?