首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求好手帮忙看一下,用fopen打不开txt文件

2013-04-05 
求高手帮忙看一下,用fopen打不开txt文件# includestdio.h# includestdlib.hstruct stu{char name[20]

求高手帮忙看一下,用fopen打不开txt文件
# include<stdio.h>
# include<stdlib.h>
struct stu
{
    char name[20];
int num;
char sex;
int age;
char adr[30];
int sal;
char health[10];
char wh[10];
};
void main()
{
struct stu s[6];
int i=0;
FILE *hp=NULL,*hp1=NULL;
    if((hp=fopen("C:\\Users\\admin\\Desktop\\stu.txt","r"))==NULL)
{
printf("open error");
exit(0);
}

    if((hp1=fopen("txt1","wb"))==NULL)
{
printf("error");
exit(0);
}

   for(i=0;i<6;i++)
   {
  fread(&s[i],sizeof(struct stu),1,hp);
   printf("%s %d %c %d %s %d %s %s",s[i].name,s[i].num,s[i].sex,s[i].age,s[i].adr,s[i].sal,s[i].health,s[i].wh);
  fwrite(&s[i],sizeof(struct stu),1,hp);
   }
  
fclose(hp);
fclose(hp1);
}

[解决办法]
建议你通过打印信息查看是什么错误造成了不能打开文件,如下:


if((hp1=fopen("txt1","wb"))==NULL)
{
    fprintf(stderr, "%s\n", strerror(errno));
    exit(1);
}

需要包含头文件errno.h
[解决办法]
如果文件名不是绝对路径的话,那是相对exe所在的当前目录;但是如果是VS的话,那应该是相对于项目所在的目录。

热点排行