Error copying and pasting data from a file to another

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,

Normal behavior

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

Abnormal behavior

And here is the beginning of the second file,

Begin 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.

+4
source share
1 answer

Sorry to confuse this question, but I really like the question, which is marked as an answer. JoachimPileborg's answer seems to have worked for you:

In addition, instead of searching and checking the size and allocation of memory, why not only, for example, data_out <data_in.rdbuf () ;? This will copy the entire input file to the output. - Joachim Pileborg July 29 at 17:26

Link http://www.cplusplus.com/reference/ios/ios/rdbuf/ and an example:

 #include <fstream> #include <string> #include <vector> int main(int argc, char** argv) { typedef std::vector<std::string> Filenames; Filenames vecFilenames; // Populate the list of file names vecFilenames.push_back("Text1.txt"); vecFilenames.push_back("Text2.txt"); vecFilenames.push_back("Text3.txt"); // Merge the files into Output.txt std::ofstream fpOutput("Output.txt"); for (Filenames::iterator it = vecFilenames.begin(); it != vecFilenames.end(); ++it) { std::ifstream fpInput(it->c_str()); fpOutput << fpInput.rdbuf(); fpInput.close(); } fpOutput.close(); return 0; } 
0
source

Source: https://habr.com/ru/post/1494088/


All Articles