C++ 编写一个复制文件的程序

版权声明:博客注明来源即可。 https://blog.csdn.net/u014027680/article/details/83005019

来源:我的博客站 OceanicKang |《C++ 编写一个复制文件的程序》

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	if (argc != 3) {
		
		cerr << "格式为: ./test [copy_file_name] [create_file_name]" << endl;
		return 0;
		
	}
	
	ifstream in(argv[1]);
	if (!in) {
		
		cerr << "打开 " << argv[1] << " 文件失败" << endl;
		return 0;
		
	}
	
	ofstream out(argv[2]);
	if (!out) {
		
		cerr << "创建 " << argv[2] << " 文件失败" << endl;
		in.close(); // 擦屁股
		return 0;
		
	}
	
	char x;
	while (in >> x) out << x;
	
	out << endl;
	
	in.close();
	out.close();
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u014027680/article/details/83005019