unexpected results with ofstream

The following file should take the contents of input_file and concatenate it to out_file. But when I run it out_file and the collection (tmpO) file are empty.

#include <iostream>
#include <fstream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void fileconcat(string input_file, string out_file, string dir)
cout << "inp" << input_file << " out" << out_file << endl;
string tmpout = dir + "/tmpO";
ifstream in1;
ifstream in2;
ofstream out;

in1.open(input_file.c_str(), ios::in | ios::binary);
in2.open(out_file.c_str(), ios::in | ios::binary);
out.open(tmpout.c_str(), ios::out | ios::app | ios::binary);

out << in2.rdbuf();
out << " " << flush;
out << in1.rdbuf();
rename(tmpout.c_str(), out_file.c_str());


int main (int argc, char* argv[])
fileconcat(argv[1], argv[2], getcwd(NULL, 0));

return 0;
First, please use code tags - the <> button on the right.

Next, move this post to "General C++ Programming" page.

You say you have run the program - but I doubt it would compile because you haven't inluded the string header.
After I run this program out_file contains the content of input_file + the content of out_file before the program ran. Only reason out_file would be empty is if input_file and out_file is empty or non-existing before the program run. There is no file called tmpO because you did change its name.
Ok Peter - you seem to have got me again - how does this run without the string header?
The only header I had to add was <unistd.h> to be able to use getcwd. In GCC, <string> is included in <iostream>, but I still think it's a good idea to include <string> because this might change in future versions of GCC or if you change to another compiler.
Last edited on
Ok, thanks Peter.

Topic archived. No new replies allowed.