Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- using namespace std;
- FILE *fl;
- int main()
- {
- if ( (fl = fopen("test.dat", "wb")) == NULL) // creating
- {
- cout << "file is NOT created";
- }
- else
- {
- cout << " file is created";
- }
- cout << endl;
- if ((fl = fopen("test.dat", "ab")) == NULL) // writing
- {
- cout << "file NOT writed";
- }
- else
- {
- cout << "file writed";
- }
- cout << endl;
- int k;
- char ch[10]="yes" ;
- while (strcmp(ch,"yes")==0)
- {
- cout << "which number u wanna to write in file: ";
- cin >> k;
- if (fwrite(&k, sizeof(int), 1, fl) == 1)
- {
- cout << "1 element writed" << endl<<endl;
- }
- cout << "Do u want continue to write?\n yes / no"<<endl;
- cin >> ch;
- }
- cout << endl;
- fclose(fl);
- if ((fl = fopen("test.dat", "rb")) == NULL) // reading fl
- {
- cout << "file NOT readed";
- }
- else
- {
- cout << "file readed";
- }
- int max = 0;
- int min = 0;
- fread(&max, sizeof(int), 1, fl); //max
- rewind(fl);
- fread(&min, sizeof(int), 1, fl); //min
- rewind(fl);
- int pos = 0;
- int max_pos = 0;
- int min_pos = 0;
- while (fread(&k, sizeof(int), 1, fl) == 1)
- {
- if (k > max)
- {
- max = k;
- max_pos = pos;
- }
- else
- {
- min = k;
- min_pos = pos;
- }
- pos++;
- }
- fclose(fl);
- int kolvo;
- cout << endl;
- kolvo = fabs(max_pos - min_pos)-1;
- cout << "element between max and min: "<< kolvo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement