您的位置:首页 > 编程语言 > C语言/C++

c++ 如何将一个文件夹里的所有文件追加到另一个文件中

2012-11-10 16:49 615 查看

方法:

1、打开A文件,准备追加信息。

fin.open(file_Name, ios_base::app);

2、依次打开文件夹中的文件,将内容追加到A中。

使用FindFirstFile()跟FindNextFile();

VC声明

HANDLE FindFirstFile(

LPCTSTR lpFileName, // file name

LPWIN32_FIND_DATA lpFindFileData // data buffer);

参数说明

HANDLE hFindFile搜索的文件句柄 函数执行的时候搜索的是此句柄的下一文件 LPWIN32_FIND_DATA lpFindFileData 指向一个用于保存文件信息的结构体

返回值

如果调用成功返回一个句柄,可用来做为FindNextFileFindClose参数

调用失败 返回为INVALID_HANDLE_VALUE(即-1) ,可调用GetLastError来获取错误信息

VC声明

BOOLFindNextFile(

HANDLE hFindFile, //searchhandle

LPWIN32_FIND_DATA lpFindFileData //databuffer );

功能说明

继续查找FindFirstFile函数搜索后的文件

参数说明

HANDLE hFindFile搜索的文件句柄 函数执行的时候搜索的是此句柄的下一文件

LPWIN32_FIND_DATA lpFindFileData 指向一个用于保存文件信息的结构体

返回值

非零表示成功,零表示失败。如不再有与指定条件相符的文件,会将GetLastError设置成ERROR_NO_MORE_FILES

总体框架:



WIN32_FIND_DATA FindFileData;


HANDLE hFind = ::FindFirstFile(pcsDir, &FindFileData);


string filename;


if(INVALID_HANDLE_VALUE == hFind)


return false;


int flag = 1;


while(flag != 0)


{


flag = FindNextFile(hFind, &FindFileData);


filename = FindFileData.cFileName;





}


FindClose(hFind);


3、需注意的地方:
目录的路径名需要:const char * pcsDir = "D:\\eng\\*.*";

源代码:

[cpp] view plaincopyprint?

#include <iostream>

#include <fstream>

#include <string>

#include <windows.h>

#include<stdio.h>

usingnamespace std;

constchar * file_Name = "listening.txt";

constchar * pcsDir = "D:\\eng\\*.*"; ////////////////////////////////////

ofstream fin;

bool CombineFile(string filename);

int iFindFiles();

int main()

{

ifstream fin_read;

fin_read.open(file_Name);

char ch;

if(!fin_read.is_open())

{

cout<< "can not open the file(fin_read)" << endl;

return 0;

}

else

{

char ch;

while (fin_read.get(ch))

cout << ch;

cout << endl <<endl;

fin_read.close();

}

string str;

cout << "if you wanna copy those files, please enter -- yes:" <<endl;

cin >>str;

if( str == "yes" )

{

fin.open(file_Name, ios_base::app);

if( !fin )

{

cout <<"can not open the fin" <<endl;

}

else

{

iFindFiles();

}

}

fin.close();

return 0;

}

int iFindFiles( )

{

if (!pcsDir)

{

cout <<"can not open the dir" << endl;

returnfalse;

}

cout << "open the dir" << endl;

WIN32_FIND_DATA FindFileData;

HANDLE hFind = ::FindFirstFile(pcsDir, &FindFileData);

string filename;

if(INVALID_HANDLE_VALUE == hFind)

returnfalse;

int flag = 1;

while(flag != 0)

{

flag = FindNextFile(hFind, &FindFileData);

filename = FindFileData.cFileName;

int strSize = filename.size();

if( strSize <= 3 )

continue;

string isStr = filename.substr(strSize-3, strSize);

if( isStr == "lrc")

{

CombineFile(filename);

}

}

FindClose(hFind);

return 0;

}

bool CombineFile(string filename)

{

ifstream foutput;

filename = "D:\\eng\\" + filename;

cout << "the f1ile name is " << endl <<filename.c_str() <<endl;

foutput.open(filename.c_str());

if( !foutput )

{

cout << "can not open the file" << endl<<endl;

returnfalse;

}

char ch;

while (foutput.get(ch))

{

fin<< ch;

}

fin << endl << endl;

foutput.close();

returntrue;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐