C语言打开一个文件的数据到另一个文件里,为什么总显示cannot open file

#include <stdio.h> /*标准输入输出函数库*/
#include <stdlib.h> /*标准函数库*/
#include <string.h> /*字符串函数库*/

结构体就省略了
int Import(void)
{
char r_file[40];
struct student stu;
FILE *fptr1,*fptr2;

printf("which file do you want to import to stu.dat?\n");
scanf("%s",&r_file);
if((fptr1=fopen("r_file","r+"))==NULL)
{
printf("cannot open %s",r_file);
return 0;
}
if((fptr2=fopen("stu.dat","a"))==NULL)
{
printf("cannot open file");
return 0;
}
while(feof(fptr1)==0)
{
memset(&stu,0,sizeof(struct student));
fgets(stu.num,11,fptr1);
fgets(stu.name,20,fptr1);
fscanf(fptr1,"%f",stu.pgrade);
fscanf(fptr1,"%f",stu.mgrade);
fscanf(fptr1,"%f",stu.egrade);
fscanf(fptr1,"%f",stu.total);

fwrite(&stu,sizeof(struct student),1,fptr2);
}
printf("sucessful import");
fclose(fptr1);
fclose(fptr2);
return 0;
}

就是你输入一个文件名,然后将数据导入到stu.dat中,这两个文件我都放在源文件的那个文件夹里了,是代码哪里错了吗?C语言

第一,确保这两个文件存在
第二,改成绝对路径试一下,也就是带盘符那种,比如D:\\xxx\\xxx这样的,应该可以
然后就是试验该放在哪里了,根据你ide不同,有可能是源文件所在路径,不过看起来不是
还可能是编译出来的exe所在路径,或者是工程文件所在路径
都试一下
其实用绝对路径是个不错的选择追问

我改了绝对路径了,还是不行

追答

文件存在吗?

温馨提示:答案为网友推荐,仅供参考
相似回答