| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 428 人关注过本帖
标题:文件的运用
取消只看楼主 加入收藏
chenqing
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-5-18
收藏
 问题点数:0 回复次数:0 
文件的运用

目的是通过文件查找
可是我在输入的时候输一次就跳回了
怎么才能把3个要输入的一次都输入
#include <stdio.h>
#define SIZE 3

struct date
{
int year;
int month;
int day;
};
struct book
{
char name[15];
char author[15];
char puthor[15];
struct date time;
float price;
int ln;
int fn;
}book[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("bk_list","w"))==NULL)
{
printf("cannot open file \n");
return;
}
for(i=0;i<3;i++)
if(fwrite(&book[i],sizeof(struct book),1,fp)!=1)
printf("file write error \n");
fclose(fp);
}
void scan()
{
int i;
printf("Input the information:\n");
printf("name author puthor year month day price ln fn \n");
for(i=0;i<SIZE;i++)
scanf("%s%s%s%d%d%d%f%d%d",book[i].name,book[i].author,book[i].puthor,&book[i].time.year,&book[i].time.month,&book[i].time.day,&book[i].price,&book[i].ln,&book[i].fn);
save();
}
void load()
{
FILE *fp;
int i;
fp=fopen("bk_list","r");
printf("name author puthor year month day price ln fn\n");
for(i=0;i<SIZE;i++)
{ fread(&book[i],sizeof(struct book),1,fp);
printf("%-10s %-15s %-15s %4d %2d %2d %f %5d %5d \n",book[i].name,book[i].author,book[i].puthor,book[i].time.year,book[i].time.month,book[i].time.day,book[i].price,book[i].ln,book[i].fn);
}
fclose(fp);
}
search1()
{
char a[10];
int i;
FILE *fp;
printf("please input book-name\n");
if((fp=fopen("bk_list","rb"))==NULL)
{
printf("cannot open infile\n");
return;
}
scanf("%s",a);
for(i=0;i<SIZE;i++)
{
fread(&book[i],sizeof(struct book),1,fp);
if(strcmp(a,book[i].name)==0)
printf("%-10s %-15s %-15s %4d %2d %2d %f %5d %5d \n",book[i].name,book[i].author,book[i].puthor,book[i].time.year,book[i].time.month,book[i].time.day,book[i].price,book[i].ln,book[i].fn);
}
fclose(fp);
}
search2()
{
char a[10];
int i;
FILE *fp;
printf("please input author-name\n");
if((fp=fopen("bk_list","rb"))==NULL)
{
printf("cannot open infile\n");
return;
}
scanf("%s",a);
for(i=0;i<SIZE;i++)
{
fread(&book[i],sizeof(struct book),1,fp);
if(strcmp(a,book[i].name)==0)
printf("%-10s %-15s %-15s %4d %2d %2d %f %5d %5d \n",book[i].name,book[i].author,book[i].puthor,book[i].time.year,book[i].time.month,book[i].time.day,book[i].price,book[i].ln,book[i].fn);
}
fclose(fp);
}

main()
{
int c;
printf("This is the book system.\n");
printf("Input 1,you can import all the information of the books.\n");
printf("Input 2,you can see all the information of the books.\n");
printf("Input 3,you can import the credit to look up the information of the books.\n");
printf("Input 4,you can import the quality to look up the information of the books.\n");
scanf("%d",&c);
switch(c)
{
case 1:scan();break;
case 2:load();break;
case 3:search1();break;
case 4:search2();break;
default:printf("error");
}
}


搜索更多相关主题的帖子: 文件 
2006-05-27 12:37
快速回复:文件的运用
数据加载中...
 
   



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

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