C语言 为什么在文件里显示乱码
#include<stdio.h>#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<string.h>
#include<conio.h>
float a[520]; //定义全局变量
int n;//函数声明
void xs();//显示
void lr();//录入
void bj();//编辑
void sc();//删除
void xg();//修改
void cr();//插入
void cx();//查询
void pl();//排序
void sx();//筛选
void tj();//添加
typedef int BOOL;
typedef struct bookinfo
{
char number[520];//ISBN
char name[520];//书名
float price;//单价
char auther[520];//作者
}BOOKINFO;
void yy()//登陆界面
{
system("cls");//清屏
printf("\n\n\t\t\t ====================================\n");
printf("\t\t == 欢 迎 进 入 图 书 管 理 系 统 ==\n");
printf("\t\t == MD丶不帅 为 你 服 务 ==\n");
printf("\t\t ========================================\n");
system("pause");
}
void mm()
{
int i=0;//记录密码次数
char s[88];
printf("\n\n\n\t\t\t欢迎来到谜底图书中心\n");
printf("\t\t\t 请您输入登陆密码:");
while(gets(s))
{
if(strcmp(s,"520")!=0)
{
system("cls");
printf("\n\n\t\t 您的输入有误,请重新输入");
i++;
if(i==3)
{
system("cls");
printf("\n\n\t\t您的错误次数达到3次,即将爆炸\n");
system("pause");
exit(0);
}
}
else
{
system("cls");
printf("\n\n\t\t 登陆成功,即将进入MD丶不帅图书信息管理系统\n");
system("pause");
break;
}
}
}
void gg()//录入新的图书信息
{
char choice;
FILE *p;
BOOKINFO newbook;
system("cls");
while(1)
{
printf("输入图书编号:");
gets(newbook.number);//输入字符串
printf("输入图书名称:");
gets(newbook.name);
printf("输入图书单价:");
scanf("%f",&newbook.price);
while(getchar()!='\n');
printf("输入图书的作者:");
gets(newbook.auther);
printf("是否保存该本图书?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
{
p=fopen("bookinfo.txt","ab");
fwrite(&newbook,sizeof(BOOKINFO),1,p);
fclose(p);
printf("\n该本书已添加到bookinfo.txt文件中!\n");
}
else
{
printf("\n本图书已保存!\n");
}
printf("\n是否继续添加图书?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
continue;
else
break;
}
}
void xs()//显示全部图书信息
{
system("cls");
FILE *p;
int n;
BOOKINFO bookinfo[100];
int booknumber=0;
system("cls");
p=fopen("bookinfo.txt","rb");
while(!feof(p))
{
fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
booknumber++;
}//booknumber--;使用feof()函数会多读一行,因此需要booknumber自减一次,使书目的数量正确
fclose(p);
booknumber--;
if(booknumber==0)
{
printf("没有任何图书信息!\n\n");
}
else
{
n=0;
printf(" 图书信息如下\n");
printf(" 图书信息 \n");
printf("编号 名称 单价 作者 \n");
while(n<booknumber)
{
printf("%-6s%-12s%-8.1f%-8s\n",bookinfo[n].number,bookinfo[n].name,bookinfo[n].price,bookinfo[n].auther);
n++;
}
}
printf("\n\n按任意键回到主菜单!\n");
getch();
}
void cx()//根据图书名称查寻图书的信息
{
system("cls");
char search[20]="";//search用来存放要查询的书名
int n;
FILE *p;
char choice;
BOOKINFO bookinfo[100];
int booknumber=0;
system("cls");
p=fopen("bookinfo.txt","rb");
while(!feof(p))
{
fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
booknumber++;
}
booknumber--;//使用feof()函数会多读一行,因此需要booknumber自减一次,使书目的数量正确
fclose(p);
while(1)
{
printf("输入要查询的书本名称:");
gets(search);
if(booknumber==0)
{
printf("书库中没有任何信息!\n\n");
printf("按任意键回到主菜单!\n\n");
getch();
break;
}//if结束
else
{
for(n=0;n<booknumber;n++)
if(strcmp(bookinfo[n].name,search)==0)
{
printf("该书的详细信息如下:\n");
printf(" 图书信息 \n");
printf("编号 名称 单价 作者 \n");
printf("%-6s%-12s%-8.1f%-8s\n",bookinfo[n].number,bookinfo[n].name,
bookinfo[n].price,bookinfo[n].auther);
break;
}
if(n>=booknumber)
printf("没有查找该书的任何信息!\n");
printf("\n\n是否继续查询?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
continue;
else
break;
system("pause");
}//else结束
}//while(1)结束
}
void bj()//编辑图书数据菜单
{
int y;
while(1)
{
system("cls");
printf("\n\n\n\n\n\t\t\t 编辑图书数据\n");
printf("\t\t\t****************\n");
printf("\t\t\t* 1.删除图书 *\n");
printf("\t\t\t* 2.插入图书 *\n");
printf("\t\t\t* 3.修改图书 *\n");
printf("\t\t\t* 0.退出编辑 *\n");
printf("\t\t\t****************\n");
printf("\t\t\t 请选择(0-3):");
scanf("%d",&y);
getchar();
switch(y)
{
case 1:
sc();break;
case 2:
cr();break;
case 3:
xg();break;
default:
return;
system("pause");
}
}
}
void sc()//根据图书名称对图书信息进行删除
{
system("cls");
char search[20]="";//search用来存放要删除的书名
int n,i;
FILE *p;
char choice;
BOOKINFO bookinfo[100];
int booknumber;
system("cls");
while(1)
{
printf("输入要删除的书本名称:");
gets(search);
p=fopen("bookinfo.txt","rb");
booknumber=0;
while(!feof(p))
{
fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
booknumber++;
}
booknumber--;//使用feof()函数会多读一行,因此需要booknumber自减一次,使书目的数量正确
fclose(p);
if(booknumber==0)
{
printf("书库中没有任何信息!\n\n");
printf("按任意键回到主菜单!\n\n");
getch();
break;
}//if结束
else
{
for(n=0;n<booknumber;n++)
if(strcmp(bookinfo[n].name,search)==0)
{
break;
}
if(n>=booknumber)
printf("没有查找该书的任何信息!\n");
else
{
printf("是否确认需要删除该本书?(Y/N)");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
{
for(i=n;i<booknumber-1;i++)
bookinfo[i]=bookinfo[i+1];
booknumber--;
p=fopen("bookinfo.txt","wb");
for(n=0;n<booknumber;n++)
fwrite(&bookinfo[n],sizeof(BOOKINFO),1,p);
fclose(p);
printf("删除成功!\n");
}
else
printf("\n\n该本书没有被删除!");
}
printf("\n\n是否继续进行删除操作?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
continue;
else
break;
system("pause");
}//else结束
}//while(1)结束
}
void cr()//插入图书
{
char choice;
FILE *p;
BOOKINFO newbook;
system("cls");
while(1)
{
printf("插入图书:\n\n");
printf("插入图书编号:");
gets(newbook.number);
printf("\n");
printf("插入图书名称:");
gets(newbook.name);
printf("\n");
printf("插入图书单价:");
scanf("%f",&newbook.price);
while(getchar()!='\n');
printf("\n");
printf("插入图书的作者:");
gets(newbook.auther);
printf("\n");
printf("是否保存图书?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
{
p=fopen("bookinfo.txt","ab");
fwrite(&newbook,sizeof(BOOKINFO),1,p);
fclose(p);
printf("\n图书已插入到bookinfo.txt文件中!\n");
}
else
{
printf("\n图书未保存!\n");
}
printf("\n是否继续插入图书?(Y/N)\n");
choice=getch();
while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
choice=getch();
if(choice=='Y'||choice=='y')
continue;
else
break;
system("pause");
}
}
void xg()
{
system("cls");
int i,k=0;
BOOKINFO bookinfo[520];
char a[520],ch[520];
printf("请输入要查询的书名:");
scanf("%s",a);
for(i=0;i<k;i++)
if(strcmp(bookinfo[i].name,a)==0)
printf(" 图书信息 \n");
printf("编号 名称 单价 作者 \n");
printf("%-6s%-12s%-8.1f%-8s\n",bookinfo[i].number,bookinfo[i].name,
bookinfo[i].price,bookinfo[i].auther);
{
printf("\n确定要改变书名(y/n):");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{
printf("\n书名:");
scanf("%s",bookinfo[i].name);
}
printf("\n确定要改变书的单价(y/n):");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{
printf("\n单价:");
scanf("%s",bookinfo[i].price);
}
printf("\n确定要改变书的作者(y/n):");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{
printf("\n作者:");
scanf("%s",bookinfo[i].auther);
}
}
system("pause");
}
void pl()//根据图书名称排列
{
system("cls");
FILE *p;
int m,n;
BOOKINFO temp;
BOOKINFO bookinfo[100];
int booknumber;
p=fopen("bookinfo.txt","rb");
booknumber=0;
system("cls");
while(!feof(p))
{
fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
booknumber++;
}
booknumber--;//使用feof()函数会多读一行,因此需要booknumber自减一次,使书目的数量正确
fclose(p);
if(booknumber==0)
{
printf("没有任何图书信息!\n\n");
}
else
{
for(m=0;m<booknumber-1;m++)
for(n=m+1;n<booknumber;n++)
if(strcmp(bookinfo[m].name,bookinfo[n].name)>0)
{
temp=bookinfo[m];
bookinfo[m]=bookinfo[n];
bookinfo[n]=temp;
}
p=fopen("bookinfo.txt","wb");
for(m=0;m<booknumber;m++)
fwrite(&bookinfo[m],sizeof(BOOKINFO),1,p);
fclose(p);
printf("\n\n完成排序!\n\n");
}
printf("按任意键回到主菜单!\n");
getch();
}
void xx()//根据价格列出图书信息
{
system("cls");
float price;
FILE *p;
int n;
int count=0;
BOOKINFO bookinfo[100];
int booknumber;
system("cls");
printf("请输入价格:");
scanf("%f",&price);
p=fopen("bookinfo.txt","rb");
booknumber=0;
while(!feof(p))
{
fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
booknumber++;
}
booknumber--;//使用feof()函数会多读一行,因此需要booknumber自减一次,使书目的数量正确
fclose(p);
if(booknumber==0)
{
printf("没有任何图书信息!\n\n");
}
else
{
for(n=0;n<booknumber;n++)
{
if(bookinfo[n].price>=price)
count++;
}
if(count==0)
printf("书库中没有比%.1f$价格高的图书!",price);
else
{
printf(" 价格高于%.1f$的图书信息如下\n",price);
printf(" 图书信息 \n");
printf("编号 名称 单价 作者 \n");
for(n=0;n<booknumber;n++)
{
if(bookinfo[n].price>=price)
printf("%-6s%-12s%-8.1f%-8s\n",bookinfo[n].number,bookinfo[n].name,bookinfo[n].price,bookinfo[n].auther);
}
}
}
printf("\n\n按任意键回到主菜单!\n");
getch();
}
void ss()
{
system("cls");
printf("\n\n\n\n\n\n\t\t\t 图书信息管理系统 \n");
printf("\t\t ☆★☆★☆★ ☆★☆★☆★\n");
printf("\t\t☆★ ☆★ ☆★ ☆★\n");
printf("\t\t☆★ 感谢 ☆★☆★ 使用 ☆★\n");
printf("\t\t☆★ 谢谢 ☆★\n");
printf("\t\t☆★ 光临 ☆★\n");
printf("\t\t ☆★ ┌─────┐ ☆★\n");
printf("\t\t ☆★ │MD丶不帅│ ☆★\n");
printf("\t\t ☆★ └─────┘ ☆★\n");
printf("\t\t ☆★ ☆★\n");
printf("\t\t ☆★ ☆★\n");
printf("\t\t ☆★ ☆★\n");
printf("\t\t ☆★ ☆★\n");
printf("\t\t ☆★☆★\n");
}
int main()
{
yy();//调用欢迎界面
mm();//调用登陆界面
int xz;
while(1)
{
system("cls");
printf("\n\n\n\n\n\t\t\t 图书信息管理系统\n");//显示主菜单
printf("\t\t\t****************\n");
printf("\t\t\t* 1.录入 *\n");
printf("\t\t\t* 2.显示 *\n");
printf("\t\t\t* 3.查寻 *\n");
printf("\t\t\t* 4.编辑 *\n");
printf("\t\t\t* 5.排序 *\n");//先完成排序之后
printf("\t\t\t* 6.排列 *\n");//按价格列出
printf("\t\t\t* 0.退出 *\n");
printf("\t\t\t****************\n");
printf("\t\t\t 请选择(0-6):");
scanf("%d",&xz);
getchar();
switch(xz)
{
case 1:
gg();break;//录入新的图书信息
case 2:
system("cls");
printf("\n");
xs();
system("pause");
break;//显示全部图书信息
case 3:
cx();break;//根据书名查寻
case 4:
bj();break;//编辑图书
case 5:
pl();break;//根据图书名称排列
case 6:
xx();break;//根据价格列出图书信息
case 0:
ss();//调用结束界面
exit(0);
}
}
return 0;
}