I am writing code to combine multiple text files and output a single file. There can be up to 22 input text files that contain 1400 lines. Each line has 8 bits of binary code and new line characters \ n. I exit a single file that combines all 22 text files.
The problem is with my output file, after 1400 lines it seems that the content from the previous file is still placed in the output file (although the length of the previous file was 1400 lines). This additional content also begins to have additional space between each line if it is open in a Microsoft office or sublime, however it is interpreted as one line if it is opened with notepad or excel (one cell in excel).
Below is an image of the expected behavior of the output file,

Here is a picture of abnormal behavior. It starts when the first file ends. I know that this data is obtained from the first file, since the second file starts with 00000000

And here is the beginning of the second file,

And this abnormal behavior is repeated every time the files are switched.
My implementation to achieve this is as follows:
repeat: if(user_input == 'y') { fstream data_out ("data.txt",fstream::out); for(int i = 0; i<files_found; i++) { fstream data_in ((file_names[i].c_str()),fstream::in); if(data_in.is_open()) { data_in.seekg(0,data_in.end); long size = data_in.tellg(); data_in.seekg(0,data_in.beg); char * buffer = new char[size]; cout << size; data_in.read(buffer,size); data_out.write(buffer,size); delete[] buffer; }else { cout << "Unexpected error"; return 1; } data_in.close(); } data_out.close(); }else if(user_input == 'n') { return 1; }else { cout << "Input not recognised. Type y for Yes, and n for No"; cin >> user_input; goto repeat; }
Additional Information:
I checked the size variable and, as I expect, 14000. 8 bits and a \ with n = 10 characters per line, 1400 lines x 10 = 14000.
Assuming a code reader will be tested.
user1167219
source share