Since there are often many random numbered files that are read in a folder, many times it is necessary to read all the files in certain formats.
The following code can read all files and folders in the specified file home in JPG-formatted files
Reference: http://www.2cto.com/kf/201407/316515.html
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace Std;
?
?
Get all the file names
void Getallfiles (string path, vector<string>& files)
{
?
???? Long hfile = 0;
???? File information
???? struct _finddata_t FileInfo;
???? string P;
???? if (hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))!! =-1)
???? {
???????? Do
???????? {
???????????? if ((Fileinfo.attrib & _a_subdir))
???????????? {
???????????????? if (strcmp (Fileinfo.name, ".")! = 0 && strcmp (fileinfo.name, "...")! = 0)
???????????????? {
???????????????????? Files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
???????????????????? Getallfiles (p.assign (Path). append ("\ \"). Append (Fileinfo.name), files);
????????????????}
????????????}
???????????? Else
???????????? {
???????????????? Files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
????????????}
?
????????} while (_findnext (hfile, &fileinfo) = = 0);
?
???????? _findclose (hfile);
????}
?
}
?
Get file names in a specific format
void Getallformatfiles (string path, vector<string>& files,string format)
{
???? File handle
???? Long hfile = 0;
???? File information
???? struct _finddata_t FileInfo;
???? string P;
???? if (hfile = _findfirst (p.assign (Path). Append ("\\*" + format). C_STR (), &fileinfo))! =-1)
???? {
???????? Do
???????? {
???????????? if ((Fileinfo.attrib & _a_subdir))
???????????? {
???????????????? if (strcmp (Fileinfo.name, ".")! = 0 && strcmp (fileinfo.name, "...")! = 0)
???????????????? {
???????????????????? Files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
???????????????????? Getallformatfiles (p.assign (Path). append ("\ \"). Append (Fileinfo.name), Files,format);
????????????????}
????????????}
???????????? Else
???????????? {
???????????????? Files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
????????????}
????????} while (_findnext (hfile, &fileinfo) = = 0);
?
???????? _findclose (hfile);
????}
}
?
The function has two parameters, the first is a path string (string type, preferably an absolute path);
The second parameter is a folder with a file name that stores variables (vector type, reference pass).
Call the format in the main function (and save the result in the file "AllFiles.txt", the total number of first actions):
?
int main ()
{
???? String FilePath = "Testimages\\water";
???? vector<string> files;
???? char * distall = "AllFiles.txt";
?
???? Read all files, including sub-files
???? Getallfiles (FilePath, files);
?
???? Read all files in JPG format
???? string format = ". jpg";
???? Getallformatfiles (FilePath, Files,format);
???? Ofstream ofn (Distall);
???? int size = Files.size ();
???? ofn<<size<<endl;
???? for (int i = 0;i<size;i++)
???? {
???????? ofn<<files[i]<<endl;
???????? cout<< Files[i] << Endl;
????}
???? Ofn.close ();
???? return 0;
}
C + + reads all files in a home or a file with a specific suffix