Good day. I have a program

//запись в файл time_t temp = time(NULL); srand(temp); ofstream out("d:\\txtFile.txt", ios::binary | ios::out); for (int i = 0; i < n; i++) { tmp = rand() % 10001 - 5000; out.write((char*)&tmp, sizeof(tmp)); cout << tmp << " "; } out.close(); cout << endl << endl; // чтение и поиск min, max int min = 6000, max = -6000; ifstream in("d:\\txtFile.txt", ios::binary | ios::in); for (int i = 0; i < n; i++) { in.read((char*)&tmp, sizeof(tmp)); if (tmp < min) min = tmp; if (tmp > max) max = tmp; } cout << "min=" << min << endl; cout << "max=" << max << endl<<endl; in.close(); //меняем местами минимальный и максимальный элементы ... system("pause"); 

But I do not understand how to swap min and max already directly in the file. I tried to write numbers from a file into a vector and into an array, but as a result, a completely different number is obtained in the vector and in the array. Help me please. Thank.

Here is the code of the vector

 std::vector<char>numbers; int number; while (in1 >> number) { numbers.push_back(number); } cout << number; 

In it, I, of course, do not change the minimum and maximum elements yet, but in the output itself -858993460 is obtained, which is not correct


Yes, this is all the vector code, the main thing for me is to write the numbers from the file into it, and then in principle it is clear what to do. If you need to add something into it, then tell me what it is, I just do not know what else is needed to write to the vector)

  • The idea is correct. Show the code with the vector, most likely the problem is there. PS But in general, mixing time_t and int is already crooked. - PinkTux
  • Add additional information to the question by editing it (link to править under the body of the question), rather than adding as an answer. - αλεχολυτ

0