Problem in binary files in turbo c++?

I am working on binary files in turbo c++ 3.5 and i want to create a library program. I want to add information about books in a binary file and do functions such as: Search and replace, delete a record, and etc.

I do this functions but i have 2 problems: 1. For example when i add 6 records about books to file, BooksReport function cant show all records and for example just show 4 or 5 records and when i search records, from 5 records, for example i just found 3 or 2 records. 2.When i search and replace a word on file, all records thats before this edited record, will be deleted.

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);
}
Last edited on
Use a C Compiler to compile C Code
Please read my ask. My codes is c++ and work successful with turbo c++ compiler. but this have 2 bugs that i am want to solve its.

thanks.
Topic archived. No new replies allowed.