文件的修改
我的文件小程序,我截取了其中一部分,我不明白的是为什么我调用modify后,原来a中输入的数据格式就不对了呢?请高手指点一开始创建的 产品名称 品牌 产品型号 进价 库存数量
bijiben lenovle g450 5000 50
bijibne lenovle k450 5000 10
biji lian v450 5000 20
调用modify后 ,对第一行的库存数量进行修改:
产品名称 品牌 产品型号 进价 库存数量
bijiben lenovle g450 5000 60
bijibne lenovle k450 5000 10
biji lian v450 5000 20
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
struct computer // 产品信息结构体
{
char name[20];
char company[20];
char xinghao[20];
int inprice;
int number;
}one;
FILE *fp;
void creat0()//创建产品信息文件头
{
if((fp=fopen("a","w+"))==NULL)
{printf("不能建立该文件\n");exit(0);}
fprintf(fp,"%20s%20s%20s%20s%20s\n","产品名称","品牌","产品型号","进价","库存数量");//输入表头
fclose(fp);
}
void creat() //1.创建产品信息文件
{
char c='y';
if((fp=fopen("a","a+"))==NULL) //创建产品文件a并打开
{
printf("\n不能建立文件");
exit(0);
}
printf("请输入数据,产品名称,品牌,产品型号,进价,库存数量\n");
while(c=='y'||c=='Y') //判断是否继续输入
{
scanf("%s%s%s%d%d",one.name,,one.xinghao,&one.inprice,&one.number);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name,,one.xinghao,one.inprice,one.number);//将数据输入文件a
printf("继续输入请按y,退出请按n\n");
getchar();
c=getchar();
}
fclose(fp);//关闭文件a
}
void modify() //4.修改笔记本产品信息文件
{
int k=0,m;
char namel[20],xinghaol[20],c1='y',c2;
long offset;
while(c1=='y'||c1=='Y') //判断是否继续修改数据
{
if((fp=fopen("a","r+"))==NULL)//打开文件a
{
printf("无法打开文件\n");exit(0);
}
printf("请输入要修改的产品的名称和型号\n");
scanf("%s%s",namel,xinghaol);
fscanf(fp,"%s%s%s%s%s\n",&one.name,&,&one.xinghao,&one.name,&one.name);//从文件读出表头汉字
do //查找要修改的一行
{
offset=ftell(fp);
{if(offset==-1L)
printf("error\n");}
fscanf(fp,"%s%s%s%d%d\n",&one.name,&,&one.xinghao,&one.inprice,&one.number);
if((!strcmp(xinghaol,one.xinghao))&&(!strcmp(namel,one.name)))
{
k=1;
break;
}
}while((!feof(fp)));
if(k)
{
printf("已查到记录为\n");
printf("%20s%20s%20s%20d%20d\n",one.name,,one.xinghao,one.inprice,one.number);//输出要修改的信息
printf("确实要修改请按y,否则请按n\n ");
getchar();
c2=getchar();
if(c2=='y'||c2=='Y')//判断是否修改
{fseek(fp,offset,SEEK_SET); //把指针指到要修改的选项
printf("请选择要修改的选项\n");
printf("1.产品名称 2.品牌或厂商\n");
printf("3.产品型号 4 进价 \n");
printf("5.库存数量 \n");
printf("请输入要修改的选项\n");
scanf("%d",&m);
printf("请输入要修改的数据\n");
fseek(fp,offset,SEEK_SET);
switch(m)
{
case 1: printf("请输入产品名称\n");
scanf("%s",one.name);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name, ,one.xinghao,one.inprice ,one.number);
break;
case 2: printf("请输入品牌\n");
scanf("%s",);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name, ,one.xinghao,one.inprice ,one.number);
break;
case 3: printf("请输入产品型号\n");
scanf("%s",one.xinghao);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name, ,one.xinghao,one.inprice ,one.number);
break;
case 4: printf("请输入进价\n");
scanf("%d",&one.inprice);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name, ,one.xinghao,one.inprice ,one.number);
break;
case 5: printf("请输入产品数量\n");
scanf("%d",&one.number);
fprintf(fp,"%20s%20s%20s%20d%20d\n",one.name, ,one.xinghao,one.inprice ,one.number);
break;
default :printf("error\n");
}
}
}
else printf("\n输错误,无此产品。\n");
printf("继续修改数据请按y,退出请按n \n");
fclose(fp);//关闭文件a
getchar();
c1=getchar();
}
}
void main()
{
creat0();
creat();
modify();
}
[ 本帖最后由 易哓天 于 2010-9-27 19:47 编辑 ]