求大神帮忙~
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define Max 30
typedef struct {
long int Nub;
char BookName[Max];
char AuthorName[Max];
long int FLH;
char PubUnit[Max];
char PubTime[Max];
char price[Max];
}Node;
int Input( char FileName[])
{
Node G;
int i = 0;
FILE * fp ;
fp = fopen(FileName, "a+");
printf("请输入登录号\n");
scanf("%ld", &G.Nub);
getchar();
printf("请输入书名\n");
gets(G.BookName);
printf("请输入作者名\n");
gets(G.AuthorName);
printf("请输入分类号\n");
scanf("%ld", &G.FLH);
printf("请输入出版单位\n");
getchar();
gets(G.PubUnit);
printf("请输入出版时间\n");
gets(G.PubTime);
printf("请输入价格\n");
gets(G.price);
printf("%s\n", G.price);
fwrite(&G, sizeof(G), 1, fp);
fclose(fp);
return 0;
}
int Print(char FileName[])
{
Node book;
FILE * fp ;
fp = fopen(FileName, "a+");
while(1){
book.AuthorName[0] = '@';
fread(&book, sizeof(book), 1, fp);
if(book.AuthorName[0] == '@'){
break;
}
printf(" 登录号:%ld\n", book.Nub);
printf(" 书名:%s\n", book.BookName);
printf(" 作者名:%s\n", book.AuthorName);
printf(" 分类号:%ld\n", book.FLH);
printf(" 出版单位:%s\n", book.PubUnit);
printf(" 出版时间:%s\n", book.PubTime);
printf(" 价格:%s\n", book.price);
printf("-------data-------\n");
}
fclose(fp);
return 0;
}
void Delet();
int Revise(char FileName[])
{
int i = 0, j;
Node book, newbook;
printf("请输入想要修改的图书的登录号\n");
scanf("%ld", &newbook.Nub);
FILE * fp ;
fp = fopen(FileName, "r+");
while(1){
book.AuthorName[0] = '@';
fread(&book, sizeof(book), 1, fp);
if(book.AuthorName[0] == '@'){
printf("Have not found! please try again !\n");
return 0;
}
i++;
if(newbook.Nub == book.Nub){
break;
}
}
printf("%d\n", i);
FILE * fq ;
fq = fopen(FileName, "r+");
if(i == 1){
printf("请输入新的登录号\n");
scanf("%ld", &newbook.Nub);
printf("%ld\n", newbook.Nub);
getchar();
printf("请输入新的书名\n");
gets(newbook.BookName);
printf("请输入新的作者名\n");
gets(newbook.AuthorName);
printf("请输入新的分类号\n");
scanf("%ld", &newbook.FLH);
printf("请输入新的出版单位\n");
getchar();
gets(newbook.PubUnit);
printf("请输入新的出版时间\n");
gets(newbook.PubTime);
printf("请输入新的价格\n");
gets(newbook.price);
fwrite(&newbook, sizeof(newbook), 1, fq);
fclose(fq);
printf("修改成功!!!\n");
}
if(i != 1){
fseek(fq, (i-1)*sizeof(newbook)+i-1, 0);
printf("请输入新的登录号\n");
scanf("%ld", &newbook.Nub);
printf("%ld\n", newbook.Nub);
getchar();
printf("请输入新的书名\n");
gets(newbook.BookName);
printf("请输入新的作者名\n");
gets(newbook.AuthorName);
printf("请输入新的分类号\n");
scanf("%ld", &newbook.FLH);
printf("请输入新的出版单位\n");
getchar();
gets(newbook.PubUnit);
printf("请输入新的出版时间\n");
gets(newbook.PubTime);
printf("请输入新的价格\n");
gets(newbook.price);
fwrite(&newbook, sizeof(newbook), 1, fq);
fclose(fq);
printf("修改成功!!!\n");
}
// }
}
int Search(int a, char FileName[])
{
if(a == 1){
char FileName2[Max];
printf("请输入要查找的书名\n");
getchar();
gets(FileName2);
Node book;
FILE * fp ;
fp = fopen(FileName, "r");
while(1){
book.AuthorName[0] = '@';
fread(&book, sizeof(book), 1, fp);
if(book.AuthorName[0] == '@'){
printf("None\n");
break;
}
if(strcmp(FileName2, book.BookName) == 0){
printf(" Have found Sucessfully !!!\n");
printf(" 登录号:%ld\n", book.Nub);
printf(" 书名:%s\n", book.BookName);
printf(" 作者名:%s\n", book.AuthorName);
printf(" 分类号:%ld\n", book.FLH);
printf(" 出版单位:%s\n", book.PubUnit);
printf(" 出版时间:%s\n", book.PubTime);
printf(" 价格:%s\n", book.price);
printf("-------data-------\n");
break;
}
}
}
if(a == 2){
char AuthorName2[Max];
printf("请输入要查找的作者名\n");
getchar();
gets(AuthorName2);
Node book;
FILE * fp ;
fp = fopen(FileName, "r");
while(1){
book.BookName[0] = '@';
fread(&book, sizeof(book), 1, fp);
if(book.BookName[0] == '@'){
printf("None\n");
break;
}
if(strcmp(AuthorName2, book.AuthorName) == 0){
printf(" Have found Sucessfully !!!\n");
printf(" 登录号:%ld\n", book.Nub);
printf(" 书名:%s\n", book.BookName);
printf(" 作者名:%s\n", book.AuthorName);
printf(" 分类号:%ld\n", book.FLH);
printf(" 出版单位:%s\n", book.PubUnit);
printf(" 出版时间:%s\n", book.PubTime);
printf(" 价格:%s\n", book.price);
printf("-------data-------\n");
break;
}
}
fclose(fp);
return 0;
}
}
int main()
{
int i, m = 1;
Node G;
while(m = 1){
printf("------图书信息管理系统------ \n1. 录入 2.输出 3.删除 4.修改 5.查询 0.退出\n请输入命令符\n------------end-------------\n" );
scanf("%d", &i);
switch (i)
{
case 1:
char FileName[Max];
printf("请输入要录入到的文件名\n");
getchar();
gets(FileName);
Input(FileName);
break;
case 2:
Print( FileName);
break;
case 3:
// Delet();
break;
case 4:
Revise(FileName);
break;
case 5:
int a;
printf("按书名查找请输入'1',按作者名查找请输入'2' \n");
scanf("%d", &a);
Search(a, FileName);
break;
case 0:
m = 0;
exit(1);
default:
break;
}
}
getchar();
return 0;
}
在实现修改这个功能时,会出现这样个情况:当我把第二块数据块改掉,后面的数据块在实现输出功能的时候就会出现错位,求大神帮忙啊,我已经纠结了好几天了~~ 另外我不太了解fopen"r+"这种打开方式,能给我讲讲fwrite是如何通过它怎样存储数据的吗?