Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include<iomanip>
  9.  
  10. using namespace std;
  11. FILE* fl;
  12.  
  13.  
  14. struct libr
  15. {
  16. char name[10];
  17. char nazv[40];
  18. int age;
  19. int sheets;
  20. }libb;
  21.  
  22. void stars();
  23.  
  24. void creating();
  25. void filling(int* counting);
  26. void reading(int* counting);
  27. void checking(int* counting);
  28. void in_file();
  29. int menu();
  30.  
  31. int main()
  32. {
  33. int counting = 0;//skolko mi zapisali
  34.  
  35. cout << endl << "start" << endl;
  36.  
  37. while (true)
  38. {
  39. switch (menu())
  40. {
  41. cout << "viberite:" << endl;
  42. case 1:creating();
  43. break;
  44. case 2: filling(&counting);
  45. break;
  46. case 3: reading(&counting);
  47. break;
  48. case 4: checking(&counting);
  49. break;
  50. case 5: in_file();
  51. case 6: return 0;
  52. default: "Viberete normalniy variant";
  53. }
  54. }
  55. }
  56.  
  57. int menu()
  58. {
  59. int i, pi = 0;
  60. cout << "1. Sozdanie" << endl;
  61. cout << "2. Zapiz" << endl;
  62. cout << "3. Pokazat'" << endl;
  63. cout << "4.Proverit'" << endl;
  64. cout << "5.V fail" << endl;
  65. cout << "6.Zakonchit;" << endl;
  66. int k; cin >> k;
  67. return k;
  68. }
  69.  
  70. void stars()
  71. {
  72. for (int i = 0; i < 50; i++)
  73. {
  74. cout << "*";
  75. ++i;
  76. }
  77. cout << endl << endl;
  78. }
  79.  
  80. void creating()//sozdanie faila
  81. {
  82. if ((fl = fopen("test16.bin", "wb")) == NULL)
  83. {
  84. cout << "\nerror with creatin\n";
  85. }
  86. else
  87. {
  88. cout << endl << "sucs creating" << endl;
  89. }
  90. }
  91.  
  92. void filling(int* counting)//vvod dannih v fail
  93. {
  94. if ((fl = fopen("test16.bin", "ab+")) == NULL)
  95. {
  96. cout << "error with writing";
  97. }
  98. else
  99. {
  100. cout << endl << "File is redy to be filed" << endl << endl;
  101. char contin[3];
  102.  
  103. do
  104. {
  105. ++* counting;
  106. cout << "enter author: ";
  107. cin >> libb.name;
  108. cout << "enter nazvanie: ";
  109. cin >> libb.nazv;
  110. cout << "age of publish: ";
  111. cin >> libb.age;//проверка, чтобы год не было 2020+
  112. while (libb.age > 2020)
  113. {
  114. cout << "Error: neverniy god" << endl;
  115. cout << "age of publish : ";
  116. cin >> libb.age;
  117. }
  118. cout << "How many sheets: ";
  119. cin >> libb.sheets;
  120.  
  121. fwrite(&libb, sizeof(libr), 1, fl); //writing
  122.  
  123. cout << endl << endl << "contin? (1/0)" << endl;
  124. cin >> contin;
  125.  
  126. } while (strcmp(contin, "1") == 0);
  127. }
  128. cout << endl << "Kol-vo zapisan avtorov: " << *counting << endl;
  129. fclose(fl);
  130. }
  131.  
  132. void reading(int* counting)//vivod dannih iz faila
  133. {
  134. if ((fl = fopen("test16.bin", "rb+")) == NULL)
  135. {
  136. cout << "error with reading";
  137. }
  138. else
  139. {
  140. cout << endl << "file is ready to read" << endl;
  141.  
  142. for (int i = 0; i < *counting; i++)
  143. {
  144. stars();//funcia to krasiviy vivod
  145. fread(&libb, sizeof(libr), 1, fl);
  146. cout << "Athor: " << libb.name << endl;
  147. cout << "Nazvanie: " << libb.nazv << endl;
  148. cout << "Year: " << libb.age << endl;
  149. cout << "Sheets: " << libb.sheets << endl;
  150. stars();
  151. }
  152.  
  153. }
  154. fclose(fl);
  155. }
  156.  
  157. void checking(int* counting)
  158. {
  159. int year;
  160. if ((fl = fopen("test16.bin", "rb+")) == NULL)
  161. {
  162. cout << "error with reading";
  163. }
  164. else
  165. {
  166. cout << endl << "Info is ready to be cheaked" << endl;
  167. cout << "Enter year posle which info should be shown: ";
  168. cin >> year;
  169. while (year > 2020)
  170. {
  171. cout << endl << "OLO god eshche ne nastupil" << endl;
  172. cout << "Vvedite normalniy god";
  173. cin >> year;
  174. }
  175.  
  176. for (int i = 0; i < *counting; i++)
  177. {
  178. fread(&libb, sizeof(libr), 1, fl);
  179. if (libb.age > year)
  180. {
  181. stars();
  182. cout << "Athor: " << libb.name << endl;
  183. cout << "Nazvanie: " << libb.nazv << endl;
  184. cout << "Year: " << libb.age << endl;
  185. cout << "Sheets: " << libb.sheets << endl;
  186. stars();
  187. }
  188. }
  189. }
  190. fclose(fl);
  191. }
  192.  
  193. void in_file()
  194. {
  195. int year = 0;
  196. char ft_name[20];
  197. FILE* ft;
  198. int n = 0;
  199. cout << endl << "Vvedite nazvanie faila: ";
  200. cin >> ft_name;
  201.  
  202. if ((ft=fopen(ft_name,"wb"))==NULL)
  203. {
  204. cout <<endl<< "error mistake with creating new file" << endl;
  205. }
  206. else
  207. {
  208. cout << endl << "file secs created" << endl;
  209. }
  210. if ((ft = fopen(ft_name, "rb+")) == NULL)
  211. {
  212. cout << endl << "error mistake with reading new file" << endl;
  213. }
  214. else
  215. {
  216. cout << endl << "file secs read" << endl;
  217. }
  218. n = filelength(fileno(fl)) / sizeof(libr);
  219.  
  220. cout << "Enter year posle which info should be shown: ";
  221. cin >> year;
  222. while (year > 2020)
  223. {
  224. cout << endl << "OLO god eshche ne nastupil" << endl;
  225. cout << "Vvedite normalniy god";
  226. cin >> year;
  227. }
  228.  
  229. for (int i = 0; i < n; i++)
  230. {
  231. fread(&libb, sizeof(libr), 1, fl);
  232. if (libb.age > year)
  233. {
  234. fprintf(ft, "%s nazvanie: %d\n", libb.age, libb.nazv);
  235. }
  236. }
  237.  
  238. fclose(fl);
  239. fclose(ft);
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement