| prw8864 (6) | |
|
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(); in1.close(); in2.close(); out.close(); remove(out_file.c_str()); rename(tmpout.c_str(), out_file.c_str()); } int main (int argc, char* argv[]) { fileconcat(argv[1], argv[2], getcwd(NULL, 0)); return 0; } | |
|
|
|
| TheIdeasMan (1564) | |
|
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. | |
|
|
|
| Peter87 (3688) | |
| 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. | |
|
|
|
| TheIdeasMan (1564) | |
| Ok Peter - you seem to have got me again - how does this run without the string header? | |
|
|
|
| Peter87 (3688) | |
| 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
|
|
| TheIdeasMan (1564) | |
Ok, thanks Peter.ThingsLearnt++;
| |
|
|
|