sorry
am opening it with wb
and using fwrite
Code:
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#define MAX 10
struct Lib{
char Title[50];
char Author[30];
char Publisher[30];
char Year[10];
char ISBN[10];
char Edition[5];
}Lib_info[MAX];
// struct LibCard NewRec[80];
void enter(void);
void list(void);
void delete(void);
void init_list(void);
int menu_select(void);
void load (void);
void save(void);
void SearchTitle(void);
void main(void)
{
char choice;
init_list();
load();
for (;;) {
choice = menu_select();
switch(choice){
case 1:enter();
break;
case 2: delete();
break;
case 3:list();
break;
case 4:save();
break;
case 5:SearchTitle();
break;
case 6:exit(0);
}
}
}
//initialise the list
void init_list(void)
{
register int t;
for (t=0;t<MAX; ++t)Lib_info[t].Title[0] = '\0';
}
//get a menu selection
menu_select(void)
{char s [80];
int c;
printf("1.Enter\n");
printf("2.Delete\n");
printf("3.List\n");
printf("4.Save\n");
printf("5.Search\n");
printf("6.exit\n");
do{
printf("Enter choice ");
gets(s);
c = atoi(s);
}while(c<0 ||c> 6);
return c;
}
// ADD NEW ENTRIES TO THE FILE
void enter(void)
{
int slot;
char s[80];
slot = find_free();
if (slot==-1){
printf("LIst Full");
return;
}
printf("Enter Title: ");
gets(Lib_info[slot].Title);
printf("Enter Author: ");
gets(Lib_info[slot].Author);
printf("Enter Publisher: ");
gets(Lib_info[slot].Publisher);
printf("Enter Year: ");
gets(Lib_info[slot].Year);
printf("Enter ISBN: ");
gets(Lib_info[slot].ISBN);
printf("Enter Edition: ");
gets(Lib_info[slot].Edition);
}
void SearchTitle (void)
{
int slot;
slot = search();
if (slot==-1){
printf("Not FOund");
return;
}
}
find_free(void)
{
register int t;
for(t=0; Lib_info[t].Title[0] && t<MAX; ++t){
if (t==MAX) return -1;{
return t;
}
}
}
search(void)
{
register int t;
char s[80];
char s2 [80];
int c;
printf("Enter TITLE: ");
gets(s);
c = atoi(s);
for(t=0; Lib_info[t].Title[0] &&t<MAX; ++t){
if (t==MAX) return -1;{
// if (Lib_info[t].Title ){
// s2 = Lib_info[t].Title[0];
// printf("entry found");
// }
return t;
}
}
}
//DELETE THE SELECTED ITEM
void delete(void)
{
register int slot;
char s[80];
printf("enter record #: ");
gets(s);
slot = atoi(s);
if (slot>=0 && slot < MAX)
Lib_info[slot].Title[0] = '\0';
}
//DISPLAY THE LIST OF ENTRIES TO THE SCREEN
void list(void)
{
register int t;
for (t=0; t<MAX; ++t) {
if (Lib_info[t].Title[0]) {
printf("%s\n",Lib_info[t].Title);
printf("%s\n",Lib_info[t].Author);
printf("%s\n",Lib_info[t].Publisher);
printf("%s\n",Lib_info[t].Year);
printf("%s\n",Lib_info[t].ISBN);
printf("%s\n",Lib_info[t].Edition);
}
}
printf("\n\n");
}
//SAVE THE LIB STRUCTURE DETAILS TO THE LIBRARY FILE
void save(void)
{
FILE *fp;
register int i;
if ((fp=fopen("LibCard.txt","wb"))==NULL){
printf("cannot open file \n");
return;
}
for (i=0; i<MAX; i++)
if (*Lib_info[i].Title)
if (fwrite(&Lib_info[i],
sizeof(struct Lib),1,fp)!=1)
printf("File Write error\n");
fclose(fp);
}
//LOAD THE DETAILS OF THE LIBRARY FILE TO THE LIB STRUCTURE
void load (void)
{
FILE *fp;
register int i;
if ((fp=fopen("LibCard.txt","rb"))==NULL){
printf("cannot open file \n");
return;
}
init_list();
for (i=0; i<MAX; i++)
if (fread(&Lib_info[i],
sizeof(struct Lib),1,fp)!=1){
if (feof(fp)) break;
printf("File Write error\n");
}
fclose(fp);
}