4

I am writing a code to merge multiple text files and output a single file. There can be up to 22 input text files which contain 1400 lines each. Each line has 8 bits of binary and the new line characters \n. I am out putting a single file that has all 22 text files merged.

Problem is with my output file, after 1400 lines it appears that the content from the previous file is still being placed into output file(although the length of the previous file was 1400 lines). This extra content also begins to have additional line space between each row if opened by microsoft office or sublime, however it is interpreted as a single line if opened by notepad or excel(a single cell in excel).

Following is the picture of expected behaviour of the output file,

Normal Behaviour

Here is a picture of abnormal behaviour. This starts when the first file finishes. I know this data is from the first file still because the second file starts from 00000000

Abnormal Behaviour

And here is the start of the second file,

Begin of the second file

And this abnormal behavior repeats every single time the files are switching.

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;
}

Further information:

I have checked the size variable and it is as I expect, 14000. 8 bits, and a \ with n = 10 characters per line, 1400 rows x 10 = 14000.

Assuming reader of code to be experienced.

4

1 回答 1

0

很抱歉提出这个问题,但我真的很喜欢标记为已回答的问题。JoachimPileborg 的回答似乎对你有用:

此外,与其寻找和检查大小并分配内存,不如直接执行例如 data_out << data_in.rdbuf();?这会将整个输入文件复制到输出。– 约阿希姆·皮勒博格 7 月 29 日 17:26

参考http://www.cplusplus.com/reference/ios/ios/rdbuf/和一个例子:

#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;
}
于 2013-09-15T08:08:36.287 回答