C + + reads all files in a home or a file with a specific suffix

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.