Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma hdrstop
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. #pragma argsused
  6. struct Library{
  7. char surname[10], initials[4], title[20], place_of_publication[20];
  8. int year;
  9. } Lib[100], LibRes[100];
  10.  
  11. void Add(FILE *file1)
  12. {
  13. int i, n;
  14. clrscr();
  15. printf("Enter number of books\n");
  16. scanf("%d", &n);
  17.  
  18. fflush(stdin);
  19. for(i=0;i<n;i++){
  20. printf("Surname: ");scanf("%s", &Lib[i].surname);
  21. printf("Initials: "); scanf("%s", &Lib[i].initials);
  22. printf("Title: "); scanf("%s", &Lib[i].title);
  23. printf("Place of publication: "); scanf("%s", &Lib[i].place_of_publication);
  24. printf("Year: "); scanf("%d", &Lib[i].year);
  25. fwrite(&Lib[i], sizeof(struct Library), 1, file1);
  26. clrscr();
  27. }
  28. }
  29.  
  30.  
  31.  
  32. void Review (FILE *file)
  33. {
  34. int i=0;
  35.  
  36. while( fread(&Lib[i], sizeof(struct Library), 1, file))
  37. {
  38. printf("Surname and initials: %s %s \n", Lib[i].surname, Lib[i].initials);
  39. printf("Title: ");puts(Lib[i].title);
  40. printf(" place of publication and year: %s %d\n", Lib[i].place_of_publication, Lib[i].year);
  41. i++;
  42. }
  43. }
  44.  
  45. void Result (FILE *file1, FILE *file2)
  46. {
  47. int i=0, j, n=0, k=0;
  48. while( fread(&Lib[i], sizeof(struct Library), 1, file1))
  49. {
  50. if(Lib[i].year == 1993)
  51. {
  52. LibRes[k]=Lib[i];
  53. k++;
  54. }
  55. i++;
  56. }
  57.  
  58. for(i=0;i<k;i++)
  59. for(j=0;j<k-1;j++)
  60. {
  61. int l=strcmp(LibRes[j].surname,LibRes[j+1].surname);
  62. if(l>=1)
  63. {
  64. struct Library t=LibRes[j];
  65. LibRes[j]=LibRes[j+1];
  66. LibRes[j+1]=t;
  67. }
  68. }
  69. file2 = fopen("Res_Library.dat", "wb");
  70. for(i=0;i<k;i++)
  71. {
  72. fwrite(&LibRes[i], sizeof(struct Library), 1, file2);
  73. }
  74. fclose(file2);
  75. }
  76. int main(int argc, char* argv[])
  77. {
  78. int i=1, n;
  79. FILE *file1, *file2;
  80. while(i)
  81. {
  82. printf("1 - New File 2 - Add to File 3 - Open 4 - Review 5 - EXIT\n");
  83. scanf("%d", &n);
  84. if(n == 1)
  85. {
  86. file1 = fopen("Library.dat", "wb");
  87. Add(file1);
  88. fclose(file1);
  89. }
  90.  
  91. if(n==2)
  92. {
  93. file1 = fopen("Library.dat", "ab");
  94. Add(file1);
  95. fclose(file1);
  96. }
  97.  
  98. if(n==3)
  99. {
  100. file1 = fopen("Library.dat", "rb" );
  101. }
  102.  
  103.  
  104. if(n==4)
  105. {
  106. file1 = fopen("Library.dat", "rb");
  107. Review(file1);
  108. fclose(file1);
  109. getch();
  110. }
  111.  
  112. if(n==5)
  113. {
  114. fclose(file1);
  115. i=0;
  116. }
  117. }
  118. file1 = fopen("Library.dat", "rb");
  119. file2 = fopen("Res_Library.dat", "wb");
  120. Result(file1, file2);
  121. fclose(file2);
  122. file2 = fopen("Res_Library.dat", "rb");
  123. Review(file2);
  124. fclose(file1);
  125. fclose(file2);
  126.  
  127. getch();return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement