I started writing my own optimizer-translator for fun code. I am new to c ++ and SID. I enter the number from the console. Example 1048575. This code writes 00 0F FF FF to the file:
#include <iostream> #include <fstream> using namespace std; int main() { int exe[100000]; ofstream ofile; ofile.open("s.bin", ios::out | ios::app | ios::binary); int n = 0; while (n < 10000) { cin >> n; int n1 = n & 255; int n2 = (n>>8) & 255; int n3 = (n>>16) & 255; int n4 = (n>>24) & 255; ofile << static_cast<char>(n4); ofile << static_cast<char>(n3); ofile << static_cast<char>(n2); ofile << static_cast<char>(n1); } ofile.close(); return 0; } I read that it is better to work with binary like this:
#include <iostream> #include <fstream> using namespace std; int n; int main() { fstream f; f.open("s.bin", ios::out | ios::app | ios::binary); while (n < 10000) { cin >> n; f.write((char*)&n, sizeof(int)); } system("pause"); return 0; } But this code writes the number in the wrong order, it turns out FF FF 0F 00. 1. How to fix the second code? 2. Or is it better to use your bike in the first? “Since I planned to control the byte order in the algorithm during generation.” 3. Why advise the second code to work with binary files? Will there be any "inconvenience" with the growth of the code if you leave the first code?