Codes:将目录下的文档所有合并成一个文档

#include <io.h>
#include <direct.h>
#include <string>
#include <fstream>
#include <iostream>

int copyfile(std::string infile, std::string outfile);

const std::string directory = "E:\\merge\\";
const std::string outfile = "E:\\merge.txt";
int main()
{
	_finddata_t file;
	intptr_t handle;
	intptr_t handleNext;
	_chdir(directory.data());
	handle = _findfirst("*.*", &file);
	handleNext = handle;

	int i = 1;
	while (handleNext != -1)
	{
		char *fileName = file.name;
		std::string completeDir =  fileName;
		completeDir = directory + completeDir;
		std::cout<<i++<<completeDir<<std::endl;
		copyfile(completeDir, outfile);

		handleNext = _findnext(handle, &file);
	}

	return 0;
}

int copyfile(std::string infile, std::string outfile)
{
	std::ifstream in(infile);
	std::ofstream out;
	out.open(outfile, std::ofstream::app);
	//out.open(outfile);
	if(out.is_open())
	{
		std::string line;
		out<<std::endl<<std::endl<<"文件名:"<<infile.substr(directory.size(),infile.size())<<std::endl;
		while(std::getline(in, line))
		{
			out<<line<<std::endl;
		}
	}else
	{
		std::cout<<"can't open "<<outfile<<std::endl;
		return 1;
	}
	in.close();
	out.close();

	return 0;		
}

参考文献:

http://blog.csdn.net/leng_que/article/details/8065787

http://www.cnblogs.com/JCSU/articles/1190685.html

http://panpan.blog.51cto.com/489034/125731

 

你可能感兴趣的:(Codes:将目录下的文档所有合并成一个文档)