Writing from existing files to a single new output file

I want to write a program that reads from three different existing files and writes their contents into a single new output file, making sure to preserve the appearance of the lines as i copy them, allowing the user to specify the name of the output file. The input files are called input1.txt, input2.txt, and input3.txt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
using namespace std;

int main() {
  string output;
  ifstream input1("input1.txt");
  ifstream input2("input2.txt");
  ifstream input3("input3.txt");
  cout<<"Enter file name: ";
  getline(cin, output);

  if (input1.fail()){
    cout<<"File error"<<endl;
  }
  else{
    string line;
    while (getline(input1, line)){
      output<<line;
      output<<endl;
    }
    cout<<"File was written"<<endl;
  }
  input1.close();

  if (input2.fail()){
    cout<<"File error"<<endl;
  }
  else{
    string line;
    while (getline(input2, line)){
      output<<line;
      output<<endl;
    }
    cout<<"File was written"<<endl;
  }
  input2.close();

  if (input3.fail()){
    cout<<"File error"<<endl;
  }
  else{
    string line;
    while (getline(input3, line)){
      output<<line;
      output<<endl;
    }
    cout<<"File was written"<<endl;
  }
  input3.close();
}
Last edited on
looks like it does that, is there an issue?
I would insert one more end of line into the files after each file is done, though. up to you, do you want a gap between each file (at least to debug it, at first?) or not?
you should close the output file.

you can do it in a lot less code if you use system calls. If this is ALL you need to do, the operating system can do this task and c++ can delegate the work to it. (as can a script or batch file)
windows:
c:\> type file1 > outputfile
c:\> type file2 > outputfile
.. etc

unix, change "type" to "cat". There isnt a dog utility, though.
if the files are exceedingly large (gigabytes), processing it in binary would be more efficient.
Last edited on
"type file1 > outputfile"
AKA copying a file :)
btw >> appends, > overwrites
Last edited on
yes there is an error on line 19 and 20
coder0101 wrote:
yes there is an error on line 19 and 20


output is a string ... it is NOT an ostream. You have not opened an output file anywhere.


You probably ought to have
#include <string>
as well.
As a windows .bat file:

1
2
3
4
@echo off
type input1.txt > %1
type input2.txt >> %1
type input3.txt >> %1


Usage (assuming called append.bat):


append output.txt

Just:

copy input1.txt+input2.txt+input3.txt output.txt /y
Last edited on
Yeah - I forgot about the + option. Doh! :)

As a Windows .bat file:

1
2
@echo off
copy input1.txt+input2.txt+input3.txt %1 /y > null


Last edited on
hah, missed output being a string. that would certainly be a big problem.
As C++ code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <vector>
#include <iostream>
#include <string>

int main()
{
	const char* const fnames[] {"input1.txt", "input2.txt", "input3.txt"};

	std::string onam;

	std::cout << "Enter name of output file: ";
	std::getline(std::cin, onam);

	std::ofstream ofs(onam, std::ios::out | std::ios::binary);

	if (!ofs)
		return (std::cout << "Cannot open output file\n"), 1;

	for (const auto& nam : fnames) {
		std::ifstream ifs(nam, std::ios::in | std::ios::binary);

		if (!ifs)
			std::cout << "Cannot open input file " << nam << '\n';
		else {
			ifs.seekg(0, std::ios::end);

			std::vector<char> data(ifs.tellg());

			ifs.seekg(0);
			ifs.read(data.data(), data.size());
			ofs.write(data.data(), data.size());
		}
	}
}

Topic archived. No new replies allowed.