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

万望大家帮小弟改一下程序!谢谢谢谢!

2012-03-09 
万望大家帮小弟改一下程序!多谢多谢!!!我下面的这段程序想实现的功能是:我已经保存了图书的信息在文件中,

万望大家帮小弟改一下程序!多谢多谢!!!
我下面的这段程序想实现的功能是:
我已经保存了图书的信息在文件中,我想对图书进行分类:先将图书分类保存在不同的文件中,然后通过用户输入的信息switch判断输出那种类型书的列表
程序如下:#include <stdio.h>
#define   n   4
#define   MAX   100
#define   SIZE   100
enum   class{Math=1,Langue,Science,Art};
enum   status{TRUE=1,FLASE};
struct   book   {
char   bname[20];
int   bnum;
enum   class   bclass;
int   bcount;
enum   status   bstatus;
int   bleft;
struct   book   *next;
}book[SIZE];
main()
{
int   i,j,h=4,n;
struct   book   Math[MAX],Langue[MAX],Science[MAX],Art[MAX];
FILE   *fp;
for(i=0;i <h;i++)
{switch(book[i].bclass)
{case   1:if((fp=fopen( "Mathinfo.dat ", "wb "))==NULL)
                {printf( "this   file   cannot   open\n ");
  exit(0);
}
for(j=0;j <MAX;j++)
{if(fwrite(&Math[j]),sizeof(struct   book),1,fp)!=1)
printf( "file   write   error!\n ");
}
fclose(fp);
break;
 
case   2:if((fp=fopen( "Langueinfo.dat ", "wb "))==NULL)
                {printf( "this   file   cannot   open\n ");
  exit(0);
}
  for(j=0;j <MAX;j++)
{if(fwrite(&Langue[j]),sizeof(struct   book),1,fp)!=1)
printf( "file   write   error!\n ");
}
fclose(fp);
break;

case   3:if((fp=fopen( "Scienceinfo.dat ", "wb "))==NULL)
                {printf( "this   file   cannot   open\n ");
  exit(0);
}
  for(j=0;j <MAX;j++)
{if(fwrite(&Science[j]),sizeof(struct   book),1,fp)!=1)
printf( "file   write   error!\n ");
}
fclose(fp);
break;

case   4:if((fp=fopen( "Artinfo.dat ", "wb "))==NULL)
                {printf( "this   file   cannot   open\n ");
          exit(0);
        }
 
        for(j=0;j <MAX;j++)
        {if(fwrite(&Art[j]),sizeof(struct   book),1,fp)!=1)
          printf( "file   write   error!\n ");
          }
        fclose(fp);
        break;
       
default:return   0;
}
}
printf( "You   want   to   see   the   class:\n ");
printf( "1--Math;\n2--Langue;\n3--Science;\n4--Art;\n ");
scanf( "%d ",&n);
switch(n)
{case   1:if((fp=fopen( "Mathinfo.dat ", "rb "))==NULL)
{printf( "the   file   cannot   open!\n ");
exit(0);
}
for(j=0;j <MAX;j++)
{fread(&Math[j],sizeof(struct   book),1,fp);
printf( "%s ",Math[j].bname);
}
fclose(fp);
break;

case   2:if((fp=fopen( "Langueinfo.dat ", "rb "))==NULL)
{printf( "the   file   cannot   open!\n ");
exit(0);
}
for(j=0;j <MAX;j++)
{fread(&Langue[j],sizeof(struct   book),1,fp);
printf( "%s ",Langue[j].bname);


}
fclose(fp);
break;

case   3:if((fp=fopen( "Scienceinfo.dat ", "rb "))==NULL)
{printf( "the   file   cannot   open!\n ");
exit(0);
}
for(j=0;j <MAX;j++)
{fread(&Science[j],sizeof(struct   book),1,fp);
printf( "%s ",Science[j].bname);
}
fclose(fp);
break;

case   4:if((fp=fopen( "Artinfo.dat ", "rb "))==NULL)
{printf( "the   file   cannot   open!\n ");
exit(0);
}
for(j=0;j <MAX;j++)
{fread(&Art[j],sizeof(struct   book),1,fp);
printf( "%s ",Art[j].bname);
}
fclose(fp);
break;

default:return   0;
}
}
想请教的是:int   i,j,h=4,n;有错
        if(fwrite(&Art[j]),sizeof(struct   book),1,fp)!=1)想这样的句子程序中用的太多,所以出现了错误,
望大家不吝赐教
谢谢!

[解决办法]
你判断反了吧
fwrite(&Art[j]),sizeof(struct book),1,fp) <0 好像才是写错误

应该 fwrite(&Art[j]),sizeof(struct book),1,fp) = sizeof ( struct book) *1 ,即写入的字节数,好像是这样

热点排行