In the program you need to replace the number in the binary file. There was a problem with the replacement function. It reads not a number, but it’s not clear that, besides, it does not move to a new position, keeping the first megadlinal value, which should not be.

for example, written to a file:

0 3 третий 2 пятый 0 первый 4 второй 1 

if I decide to add a четвёртый 2 , the deuce near the третий should change to 5.

Code:

 void swap_val(fstream&tab){ int buf; tab.open("tab.bin", ios::in | ios::out | ios::binary); for(int i = 21; i < block*(k-1); i+=block){ tab.seekg(i); tab.read((char*)&buf, 4); cout << "buf: " << buf << endl; if(buf == val){ tab.seekg(i); tab.write((char*)&val, 4); tab.seekg(block*k - 4); tab.write((char*)&buf, 4); cout << "Check" << endl; break; } } tab.close(); } 
  • one
    Judging by the content, your file is not binary, but text - ixSci
  • judging by the functions I use and the opening mode, you are mistaken - alalambda
  • Opening mode only affects the interpretation of newlines, and nothing more. And what functions do you use the file format does not affect all the more. Your text file - judging by what you are giving, the character '2' occupies exactly one byte in it, and you should treat its value as if it were text. By the way, think about what will happen if instead of a number from one character (0-9) there are more digits (10 -...). - user6550
  • Where do you see that "2" occupied one byte? I write int to 4 bytes and read the same. - alalambda
  • The text of your question shows the structure of the file, and there I see. - user6550

0