Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma hdrstop
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #pragma argsused
- struct Library{
- char surname[10], initials[4], title[20], place_of_publication[20];
- int year;
- } Lib[100], LibRes[100];
- void Add(FILE *file1)
- {
- int i, n;
- clrscr();
- printf("Enter number of books\n");
- scanf("%d", &n);
- fflush(stdin);
- for(i=0;i<n;i++){
- printf("Surname: ");scanf("%s", &Lib[i].surname);
- printf("Initials: "); scanf("%s", &Lib[i].initials);
- printf("Title: "); scanf("%s", &Lib[i].title);
- printf("Place of publication: "); scanf("%s", &Lib[i].place_of_publication);
- printf("Year: "); scanf("%d", &Lib[i].year);
- fwrite(&Lib[i], sizeof(struct Library), 1, file1);
- clrscr();
- }
- }
- void Review (FILE *file)
- {
- int i=0;
- while( fread(&Lib[i], sizeof(struct Library), 1, file))
- {
- printf("Surname and initials: %s %s \n", Lib[i].surname, Lib[i].initials);
- printf("Title: ");puts(Lib[i].title);
- printf(" place of publication and year: %s %d\n", Lib[i].place_of_publication, Lib[i].year);
- i++;
- }
- }
- void Result (FILE *file1, FILE *file2)
- {
- int i=0, j, n=0, k=0;
- while( fread(&Lib[i], sizeof(struct Library), 1, file1))
- {
- if(Lib[i].year == 1993)
- {
- LibRes[k]=Lib[i];
- k++;
- }
- i++;
- }
- for(i=0;i<k;i++)
- for(j=0;j<k-1;j++)
- {
- int l=strcmp(LibRes[j].surname,LibRes[j+1].surname);
- if(l>=1)
- {
- struct Library t=LibRes[j];
- LibRes[j]=LibRes[j+1];
- LibRes[j+1]=t;
- }
- }
- file2 = fopen("Res_Library.dat", "wb");
- for(i=0;i<k;i++)
- {
- fwrite(&LibRes[i], sizeof(struct Library), 1, file2);
- }
- fclose(file2);
- }
- int main(int argc, char* argv[])
- {
- int i=1, n;
- FILE *file1, *file2;
- while(i)
- {
- printf("1 - New File 2 - Add to File 3 - Open 4 - Review 5 - EXIT\n");
- scanf("%d", &n);
- if(n == 1)
- {
- file1 = fopen("Library.dat", "wb");
- Add(file1);
- fclose(file1);
- }
- if(n==2)
- {
- file1 = fopen("Library.dat", "ab");
- Add(file1);
- fclose(file1);
- }
- if(n==3)
- {
- file1 = fopen("Library.dat", "rb" );
- }
- if(n==4)
- {
- file1 = fopen("Library.dat", "rb");
- Review(file1);
- fclose(file1);
- getch();
- }
- if(n==5)
- {
- fclose(file1);
- i=0;
- }
- }
- file1 = fopen("Library.dat", "rb");
- file2 = fopen("Res_Library.dat", "wb");
- Result(file1, file2);
- fclose(file2);
- file2 = fopen("Res_Library.dat", "rb");
- Review(file2);
- fclose(file1);
- fclose(file2);
- getch();return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement