SHARE
TWEET

Untitled

a guest Apr 26th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //создать бинарный файл, записать в него 10 целых чисел и найти количество чисел между минимальнывм и максимальным элементом.
  2.  
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <io.h>
  10. using namespace std;
  11. void creating_file(char[], FILE *);
  12. char* fname(char[]);
  13. void recording_file(char[], int , FILE *);
  14. int result_file(char[], int, FILE*);
  15.  
  16. int main()
  17. {
  18.     FILE *fl = NULL;
  19.     char name[20];
  20.     int a = 0;
  21.     fname(name);
  22.     creating_file(name, fl);
  23.     recording_file(name, a, fl);
  24.     cout << "amount of elements =  " << result_file(name, a, fl) << endl;
  25.     return 0;
  26. }
  27. char* fname(char name[]) {
  28.     cout << "Enter is file name : ";
  29.     cin >> name;
  30.     return name;
  31. }
  32.  
  33. void creating_file(char name[], FILE *fl)
  34. {
  35.     fopen_s(&fl, name, "wb");
  36.     if (fl == NULL)
  37.     {
  38.         cout << "Error with file creating!" << endl;    return;
  39.     }
  40.     fclose(fl);
  41. }
  42. void recording_file(char name[], int a, FILE *fl)
  43. {
  44.     fopen_s(&fl, name, "ab");
  45.     if (fl == NULL)
  46.     {
  47.         cout << "Error with file opening!" << endl; return;
  48.     }
  49.     cout << "Enter items : ";
  50.     for (int i = 0; i < 10; i++) {
  51.         cin >> a;
  52.         fwrite(&a, sizeof(int), 1, fl);
  53.     }
  54.     fclose(fl);
  55. }
  56. int result_file(char name[], int a, FILE *fl) {
  57.     fopen_s(&fl, name, "rb");
  58.     if (fl == NULL) {
  59.         cout << "Error in opening file." << endl;
  60.     }
  61.     fwrite(&a, sizeof(int), 1, fl);
  62.     int min = a, max = a;
  63.     int mini = 0, maxi = 0;
  64.     for (int i = 1; i < 10; i++) {
  65.         fread(&a, sizeof(int), 1, fl);
  66.         if (a<= min) {
  67.             min = a;
  68.             mini = i;
  69.         }
  70.         else
  71.             if (a >= max) {
  72.                 max = a;
  73.                 maxi = i;
  74.             }
  75.     }
  76.     int y;
  77.     if (maxi > mini + 1) {
  78.         y = (maxi - mini) - 1;
  79.     }
  80.     else if (maxi < mini - 1) {
  81.         y = (mini - maxi) - 1;
  82.     }
  83.     else y = 0;
  84.     return y;
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top