Method:
1. Open File A and prepare additional information.
Fin. Open (file_name, ios_base: APP );
2. open the files in the folder in sequence and append the content to.
Use findfirstfile () and findnextfile ();
VC statement
Handle findfirstfile (
Lptstr lpfilename, // file name
Lpwin32_find_data lpfindfiledata // data buffer );
Parameter description
Handle hfindfile: when the file handle function is executed, it searches for the next file of this handle, lpwin32_find_data lpfindfiledata, pointing to a struct used to save file information.
Return Value
If the call succeeds, a handle is returned, which can be used as the findnextfile or findclose parameter.
If the call fails, invalid_handle_value (-1) is returned. You can call getlasterror to obtain the error message.
VC statement
Bool findnextfile (
Handle hfindfile, // searchhandle
Lpwin32_find_data lpfindfiledata // databuffer );
Function Description
Continue searching for the files searched by the findfirstfile Function
Parameter description
Handle hfindfile: The file handle to be searched when the function is executed.
Lpwin32_find_data lpfindfiledata points to a struct used to save file information
Return Value
If the value is not zero, the operation is successful. If the value is zero, the operation fails. If no file matches the specified condition, getlasterror is set to error_no_more_files.
Overall framework:
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. Notes:
The path name of the directory must be: const char * pcsdir = "D: \ Eng \\*.*";
Source code:
#include <iostream>#include <fstream>#include <string>#include <windows.h>#include<stdio.h>using namespace std;const char * file_Name = "listening.txt";const char * 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; return false;}cout << "open the dir" << endl; 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;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;return false;}char ch;while (foutput.get(ch)){fin<< ch;}fin << endl << endl;foutput.close();return true;}