새로새록

[c++]4. 두개의 텍스트파일 합치기 본문

소프트웨어융합/경희대 c++ 과제

[c++]4. 두개의 텍스트파일 합치기

류지나 2020. 7. 15. 00:18

4. 아래와 같은 두개의 다른 텍스트파일을 하나의 텍스트 파일로 합치는 프로그램을 작성하라. (단,
줄바꿈도 텍스트 파일 형식에 포함된다)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	char ch;
	ofstream ofs;
	ofs.open("output.txt");
	ifstream ifs;
	ifs.open("input1.txt");
	while (ifs.get(ch))
		ofs.put(ch);
	ifs.close();
	ifs.open("input2.txt");
	ofs.put('\n');
	ofs.put('\n');
	while (ifs.get(ch))
		ofs.put(ch);
	ifs.close();
	ofs.close();
	return 0;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	ofstream res("integrated.txt");
	ifstream s1("input1.txt");
	ifstream s2("input2.txt");
	string line;

	while (getline(s1, line)) {
		res << line << endl;
	}
	s1.close();

	res << endl;
	while (getline(s2, line)) {
		res << line << endl;
	}
	s2.close();
	res.close();

	return 0;
}