| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:文件的修改
取消只看楼主 加入收藏
易哓天
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:80
专家分:105
注 册:2010-9-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
文件的修改
我的文件小程序,我截取了其中一部分,我不明白的是为什么我调用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 编辑 ]
搜索更多相关主题的帖子: 产品 modify 
2010-09-27 19:42
易哓天
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:80
专家分:105
注 册:2010-9-20
收藏
得分:0 
寻求帮助

匈奴未灭,何以为家
2010-09-27 19:44
易哓天
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:80
专家分:105
注 册:2010-9-20
收藏
得分:0 
        产品名称                品牌            产品型号                进价            库存数量
         bijiben             lenovle                g450                5000                  50
         bijibne             lenovle                k450                5000                  10
            biji                lian                v450                5000                  20
        产品名称                品牌            产品型号                进价            库存数量
        bijiben             lenovle                g450                5000                  50
  
 biji                lian                v450                5000                  20

经过删除操作,后一行,总是到达文档第一个格,能不能急需解决一下,我以为和修改的问题一样呢,所以只发了一个,但却不一样,如果可以的话能不能顺便说一下原因;

void del() //3.删除产品文件信息
{
int k=0;
char c1='y',c2,xinghaol[10],namel[10];
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);
fprintf(fp,"%20s%20s%20s%20s%40s\n","","","","","",);//把要删除的数据改成0
}
}
else
printf("\n输错误,无此产品。\n");
fclose(fp);//关闭文件a
printf("继续删除请按y,退出请按n\n");
getchar();
c1=getchar();
}
}

[ 本帖最后由 易哓天 于 2010-9-28 13:40 编辑 ]

匈奴未灭,何以为家
2010-09-28 13:36
快速回复:文件的修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019487 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved