samedi 3 janvier 2015

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.



struct {
char name[20];
char id[2];
char publisher[20];} books, listbooks[100];
//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);

//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);




Aucun commentaire:

Enregistrer un commentaire