Advertisement
palenda21

Untitled

Feb 22nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8. FILE *fl;
  9.  
  10. int main()
  11. {
  12.     if ( (fl = fopen("test.dat", "wb")) == NULL)   // creating
  13.        {
  14.            cout << "file is NOT created";
  15.        }
  16.        else
  17.        {
  18.            cout << " file is created";
  19.        }
  20.        cout << endl;
  21.     if ((fl = fopen("test.dat", "ab")) == NULL)  // writing
  22.       {
  23.           cout << "file NOT writed";
  24.       }
  25.       else
  26.       {
  27.           cout << "file writed";
  28.       }
  29.       cout << endl;
  30.     int k;
  31.         char ch[10]="yes" ;
  32.        
  33.             while (strcmp(ch,"yes")==0)
  34.             {
  35.                 cout << "which number u wanna to write in file: ";
  36.                 cin >> k;
  37.      
  38.                 if (fwrite(&k, sizeof(int), 1, fl) == 1)
  39.                 {
  40.                     cout << "1 element writed" << endl<<endl;
  41.                 }
  42.      
  43.                 cout << "Do u want continue to write?\n yes / no"<<endl;
  44.                 cin >> ch;
  45.             }
  46.             cout << endl;
  47.             fclose(fl);
  48.      
  49.             if ((fl = fopen("test.dat", "rb")) == NULL)  // reading fl
  50.             {
  51.                 cout << "file NOT readed";
  52.             }
  53.             else
  54.             {
  55.                 cout << "file readed";
  56.             }
  57.      
  58.             int max = 0;
  59.             int min = 0;
  60.      
  61.             fread(&max, sizeof(int), 1, fl);  //max
  62.             rewind(fl);
  63.      
  64.             fread(&min, sizeof(int), 1, fl);  //min
  65.             rewind(fl);
  66.      
  67.             int pos = 0;
  68.             int max_pos = 0;
  69.             int min_pos = 0;
  70.             while (fread(&k, sizeof(int), 1, fl) == 1)
  71.             {
  72.                
  73.                 if (k > max)
  74.                 {
  75.                     max = k;
  76.                     max_pos = pos;
  77.                 }
  78.                 else
  79.                 {
  80.                     min = k;
  81.                     min_pos = pos;
  82.                 }
  83.                 pos++;
  84.                
  85.             }
  86.             fclose(fl);
  87.             int kolvo;
  88.             cout << endl;
  89.             kolvo = fabs(max_pos - min_pos)-1;
  90.             cout << "element between max and min: "<< kolvo;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement