关于文件输入输出问题,一个小问题
# include <stdio.h> # include <windows.h>
# include <stdlib.h>
# include <string.h>
struct book
{
int book_number;
char book_name[30];
float book_price;
char book_class[20];
int book_sign;
}book[30];
void InformationSearch();
void BackStage();
bool Login();
void Initialize();
void AddItem();
void ChangeItem();
void DeletItem();
void ScanItem();
void ChangePassword();
void save(FILE * fp, int i);
void main()//主函数登陆
{
printf("--------请输入登陆状态-------\n");
printf("1.游客登录\n");
printf("2.管理员登陆\n");
int i;
scanf("%d", &i);
switch(i)
{
case 1:
{
system("cls");
Sleep(800);
InformationSearch();break;
}
case 2:
{
system("cls");
Sleep(800);
BackStage();break;
}
}
}
void InformationSearch()//信息查询系统
{
}
void BackStage()//后台系统
{
if(Login())
{
return;
}
while(1)
{
printf("--------欢迎登陆图书后台管理系统--------\n");
printf("1.初始化 所有信息\n");
printf("2.添加 图书信息\n");
printf("3.修改 图书信息\n");
printf("4.删除 图书信息\n");
printf("5.查看 图书信息\n");
printf("6.管理员密码修改\n");
int i;
scanf("%d", &i);
switch(i)
{
case 1:
{
system("cls");
Sleep(800);
Initialize();
break;
}
case 2:
{
system("cls");
Sleep(800);
AddItem();
break;
}
case 3:
{
system("cls");
Sleep(800);
ChangeItem();
break;
}
case 4:
{
system("cls");
Sleep(800);
DeletItem();
break;
}
case 5:
{
system("cls");
Sleep(800);
ScanItem();
break;
}
case 6:
{
system("cls");
Sleep(800);
ChangePassword();
break;
}
}
}
}
bool Login()//打开保存密码的文件,并验证
{
FILE * fp;
int count;
char filename[] = "password";
char ch[] = "admin";
char stringIn[10];
char stringOut[10];
}
while ((fp = fopen(filename, "rb+")) == NULL)
{
printf("这是您首次使用本系统\n默认密码为admin\n");
fp = fopen(filename, "wb+") ;
fprintf(fp, "admin");
fclose(fp);
}
fscanf(fp, "%s", stringOut);
printf("请输入密码: ");
scanf("%s", stringIn);
count = strcmp(stringIn, stringOut);
if(count==0)
return false;
else
return true;
}
void Initialize()
{
char ch;
int i = 0;
FILE * fp;
fp = fopen("Data", "wb+");
do
{
printf("=================================\n");
printf("请输入第%d项图书信息\n", i+1);
printf("图书编号: ");
scanf("%d", &book[i].book_number);
ch = getchar();
printf("图书名称: ");
scanf("%s", book[i].book_name);
ch = getchar();
printf("图书价格: ");
scanf("%f", &book[i].book_price);
ch = getchar();
printf("图书类别: ");
scanf("%s", book[i].book_class);
ch = getchar();
book[i].book_sign = 1;
printf("=================================\n");
++i;
printf("是否继续录入?(Y/N)");
ch = getchar();
}while(ch=='y'&&'Y');
save(fp, i);
fclose(fp);
}
void AddItem()
{
char ch;
int i ;
FILE * fp;
if((fp = fopen("Data", "ab+"))==NULL)
{
printf("cannot open file!\n");
return;
}
for(i=0;i<30;i++)
{
fread(&book[i], sizeof(struct book), 1, fp);
if(book[i].book_number==0)
{
break;
}
}
do
{
printf("=================================\n");
printf("请输入第%d项图书信息\n", i+1);
printf("图书编号: ");
scanf("%d", &book[i].book_number);
ch = getchar();
printf("图书名称: ");
scanf("%s", book[i].book_name);
ch = getchar();
printf("图书价格: ");
scanf("%f", &book[i].book_price);
ch = getchar();
printf("图书类别: ");
scanf("%s", book[i].book_class);
ch = getchar();
book[i].book_sign = 1;
printf("=================================\n");
printf("是否继续录入?(Y/N)");
ch = getchar();
if(fwrite(&book[i], sizeof(struct book), 1, fp)!=1)//这里的问题,为什么我添加新记录的时候总是Error!?
{
printf("Error!\n");
break;
}
++i;
}while(ch=='y'&&'Y');
fclose(fp);
}
void ChangeItem()
{
}
void DeletItem()
{
}
void ScanItem()
{
FILE * fp;
int siz, j, count = 0, i = 0;
char ch;
if((fp=fopen("Data", "rb+"))==NULL)
{
printf("打开数据文件Data错误!\n");
exit(0);
}
printf("编号 名称 价格(元) 类别\n");
for(i=0;i<30;i++)
{
fread(&book[i], sizeof(struct book), 1, fp);
if(book[i].book_number==0)
{
break;
}
printf("%2d%8s%9.2f%9s", book[i].book_number, book[i].book_name, book[i].book_price, book[i].book_class);
printf("\n");
}
}
void ChangePassword()
{
}
void save(FILE * fp, int i)
{
// fprintf(fp, "%d%10s%6.2f%10s", book[i].book_number, book[i].book_name, book[i].book_price, book[i].book_class);
// fprintf(fp, "\n");
int j;
for(j=0;j<=i;j++)
{
if(fwrite(&book[j], sizeof(struct book), 1, fp)!=1)
{
printf("error!");
break;
}
}
}
[ 本帖最后由 我是胖子 于 2013-5-15 16:52 编辑 ]