Guest User

Untitled

a guest
Apr 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5.  
  6. #define ERROR_FILE_OPEN -1
  7.  
  8. int i = 0;
  9.  
  10. struct scan_info{
  11. char model[25]; // наименование модели
  12. int price; // цена
  13. double x_size; // горизонтальный размер области сканирования
  14. double y_size; // вертикальный размер области сканирования
  15. int optr; // оптическое разрешение
  16. int grey; // число градаций серого
  17. };
  18.  
  19. void output_bin(long N, struct scan_info *task_struct)
  20. {
  21. FILE *f_output = NULL;
  22.  
  23. f_output = fopen("task.bin", "rb");
  24. if (f_output == NULL){
  25. printf("Error opening file");
  26. getch();
  27. exit(ERROR_FILE_OPEN);
  28. }
  29.  
  30. fseek(f_output, 0, SEEK_SET);
  31. fread(&N, sizeof(long), 1, f_output);
  32. fseek(f_output, 4, SEEK_SET);
  33. printf("nЧитаем структуру из бианрного файлаn");
  34. printf("Число записей: ");
  35. printf("%i",N);
  36. for(i = 0; i < N; i++){
  37. fread(&task_struct[i], sizeof(struct scan_info), 1, f_output);
  38. printf("%sn",task_struct[i]);
  39. }
  40.  
  41. fclose(f_output);
  42. }
  43.  
  44. void input_bin(long N, struct scan_info *task_struct)
  45. {
  46.  
  47. FILE *f_input = NULL;
  48.  
  49. f_input = fopen("task.bin", "w+b");
  50. if (f_input == NULL){
  51. printf("Error opening file");
  52. getch();
  53. exit(ERROR_FILE_OPEN);
  54. }
  55.  
  56. fseek(f_input, 0, SEEK_SET);
  57. fwrite(&N, sizeof(long), 1, f_input);
  58. fseek(f_input, 4, SEEK_SET);
  59. for (i=0; i<N; i++){
  60. if (task_struct[i].price >= 200){
  61. fwrite(&task_struct[i], sizeof(struct scan_info), 1, f_input);
  62. }
  63. }
  64.  
  65. fclose(f_input);
  66. }
  67.  
  68. void printf_struct(long N, struct scan_info *task_struct)
  69. {
  70. printf("nПолученная структураn");
  71. for(i = 0; i < N; i++){
  72. printf("nНазвание: ");
  73. printf("%sn",task_struct[i].model);
  74. printf("Цена: ");
  75. printf("%in",task_struct[i].price);
  76. printf("Горизонтальный размер области сканирования: ");
  77. printf("%dn",task_struct[i].x_size);
  78. printf("Вертикальный размер области сканирования: ");
  79. printf("%dn",task_struct[i].y_size);
  80. printf("Оптическое разрешение: ");
  81. printf("%in",task_struct[i].optr);
  82. printf("Число градаций серого: ");
  83. printf("%in",task_struct[i].grey);
  84. }
  85. }
  86.  
  87. void scanf_struct(long N, struct scan_info *task_struct)
  88. {
  89. for(i = 0; i < N; i++){
  90. printf("nНазвание: ");
  91. scanf("%s",&task_struct[i].model);
  92. printf("Цена: ");
  93. scanf("%i",&task_struct[i].price);
  94. printf("Горизонтальный размер области сканирования: ");
  95. scanf("%d",&task_struct[i].x_size);
  96. printf("Вертикальный размер области сканирования: ");
  97. scanf("%d",&task_struct[i].y_size);
  98. printf("Оптическое разрешение: ");
  99. scanf("%i",&task_struct[i].optr);
  100. printf("Число градаций серого: ");
  101. scanf("%i",&task_struct[i].grey);
  102. }
  103. }
  104.  
  105. int main()
  106. {
  107. SetConsoleCP(1251);
  108. SetConsoleOutputCP(1251);
  109.  
  110. long N;
  111. struct scan_info *task_struct;
  112.  
  113. printf("Введите количество записей о сканерах: ");
  114. scanf("%i", &N);
  115. task_struct = (struct scan_info*)malloc(N*sizeof(struct scan_info));
  116.  
  117. scanf_struct(N, task_struct);
  118. printf_struct(N, task_struct);
  119. input_bin(N, task_struct);
  120. output_bin(N, task_struct);
  121.  
  122. return 0;
  123. }
  124.  
  125. printf("%sn",task_struct[i]);
  126.  
  127. fwrite(&N, sizeof(long), 1, f_input);
  128. //fseek(f_input, 4, SEEK_SET);
  129. for (i=0; i<N; i++){
  130. if (task_struct[i].price >= 200){
  131. fwrite(&task_struct[i], sizeof(struct scan_info), 1, f_input);
  132. }
  133. }
Add Comment
Please, Sign In to add comment