结构体数组和文件操作问题
这是在linux下GCC编的部分程序,这是部分代码,求高人指出错误。
第一个函数功能还没完善,第二个删除有错误。
时间第一啊。。。。。
struct Item /*声明Item的结构体*/
{
int Item_Code; /*项目代码*/
char Item_Name[20]; /*项目名称*/
int Targeted_sales; /*目标销售额*/
int Year_to_Date_Sales; /*年初至今销售额*/
};
struct Sales /*声明Sales结构体*/
{
int Item_Code; /*项目代码*/
int Quantity_Sold; /*已销售的数量*/
};
struct Item itm,iem,ite[1000]; /*ite[1000]根据需要可以调整*/
struct Sales sales,sale[1000];/*sale[1000]根据需要可以调整*/
FILE *fp,*fs;
void Rec_Deta()/*添加详细信息*/
{
system( "clear ");
fp=fopen( "item.dat ", "rb+ ");
fseek(fp,-sizeof(struct Item),2);/*定位至文件尾并往前移动一个数据块*/
fread(&iem,sizeof(struct Item),1,fp); /*从当前位置开始读取*/
int id,num=0,i=0,j=0;
printf( "\t________________________________________________________________\n ");
printf( "\n\t\tADD ITEM SALE DETAILS\n ");
printf( "Enter Item code: \n ");
scanf( "%d ",&id);
if(id==0) /*编号不能为零*/
printf( "Please input a true Item code!\n ");
else if(id> itm.Item_Code) /*追加判断条件:输入的编号不能比item文件里最后一个code大*/
printf( "You can not input a code bigger than %d.\n ",itm.Item_Code);
else
{
printf( "Enter Quantity Sold: \n ");
scanf( "%d ",&num);
if(num <=0) /*设定条件,数量不能小于0*/
printf( "Please input a number more than zero.\n ");
else
{
if((fs=fopen( "sales.dat ", "rb "))==NULL) /*以只读的方式打开二进制文件,若没有,则新建.有就以追加读入的方式打开二进制文件*/
fs=fopen( "sales.dat ", "wb ");
else
fs=fopen( "sales.dat ", "ab ");
sales.Item_Code=id;
sales.Quantity_Sold=num;
fwrite(&sales,sizeof(struct Sales),1,fs);/*将内容存至sale文件*/
printf( "Record written to Sales file\n ");
while(!feof(fp)) /*更新item文件数据*/
{
fread(&iem,sizeof(struct Item),1,fp);
if(iem.Item_Code==sales.Item_Code)
{
iem.Year_to_Date_Sales=sales.Quantity_Sold;
fseek(fp,(sales.Item_Code-1)*sizeof(struct Item),0);
fwrite(&iem,sizeof(struct Item),1,fp);
printf( "Record written to Item file\n ");
break;
}
}
fclose(fs);
}
}
fclose(fp);
}
void Del_Item()/*删除*/
{
system( "clear ");
int code,i=0,j=0,k=0,t=0;
printf( "\t________________________________________________________________\n ");
printf( "\n\t\tDELETE ITEM DETAILS\n ");
printf( "Enter Item code: ");
scanf( "%d ",&code);
fp=fopen( "item.dat ", "rb ");
fs=fopen( "sale.dat ", "rb ");
/*结构体数组的删除操作*/
if(fp==NULL)
{
printf( "ERROR!!!Can not find Item file!!\n ");
exit(1);
}
rewind(fp);
while(!feof(fp)) /*判断item文件中是否有这个code*/
{
fread(&itm,sizeof(struct Item),1,fp);