So I'm writing a little assignment and it requires input out of a .bin file. So I also wrote a little algorithm that transfers .txt into .bin (only numbers as per the assignment). The problem is, the function is... acting up. For some reason adds an extra byte on 12th number. No idea why. Also, obviously, breaks reading the number.
The question is, why and how do I fix that?
The algorithm itself:
ifstream txtIn;
txtIn.open("input.txt");
if (!txtIn.is_open()) {
cout << "Ошибка чтения входного .txt файла!\n";
return;
}
ofstream binOut;
long buf;
binOut.open("input.bin");
binOut.clear();
do {
txtIn >> buf;
binOut.write((char*)&buf, sizeof(long));
} while (!txtIn.eof());
txtIn.close();
binOut.close();
cout << "Конверсия завершена\n";
Input.txt:
6 6
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Input.bin:
06 00 00 00 06 00 00 00 01 00 00 00 02 00 00 00
03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00
07 00 00 00 08 00 00 00 09 00 00 00>0D<0A 00 00
00 0B 00 00 00 0C 00 00 00 0D 00 00 00 0E 00 00
//and so on//
Also, any outdated and/or archaic solutions are a part of the assignment. Unfortunately.