1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void add();
void search();
struct {
char name[20];
char id[2];
char publisher[20];} books, listbooks[100];
void main(){
add();
// search();
getch();
}
//Add
void add(){
FILE *pt;
pt=fopen("books.dat","a");
clrscr();
printf("\t Please Enter Data for new book");
printf("\n Please enter Name:");
scanf("%s", &books.name );
printf("Please enter ID:");
scanf("%s", &books.id );
printf("Please enter Publisher:");
scanf("%s", &books.publisher);
fwrite(&books, sizeof(books), 1,pt);
fclose(pt);
}
void search(){
//Search and replace
pt=fopen("books.dat","w+");
char replaceName[20];
char searchName[20];
rewind(pt);
found=0;
printf("Please enter search word \n");
scanf("%s", &searchName);
printf("Please enter replace word \n");
scanf("%s", &replaceName);
i=0;
do{
i++;
fread(&books, sizeof(books), i,pt);
if(strcmp(searchName,books.name)==0){
found=1;
strcpy(books.name,replaceName);
fwrite(&books, sizeof(books), i,pt);
break;}
}while(!feof(pt));
clrscr();
if(found==1){
printf("Replace successful!");
}
if(found==0){
printf("Not Found");
}
fclose(pt);
}
|