File IO utility library for C++

The project contain a set of C++ functions that make it easy to search for file and directory names in a given search path. Note that this function will only work on windows since it is using windows API to look for files and folders.


======================================================
(1) dir_ff function
======================================================

search for file and directory names in a given search path.
Note: this function will only work on windows since it is using windows API to look for files and folders.

search_path can be a folder:
e.g: std::string search_path = “C:\\Strawberry\\”;

Note: it must end with \\

It can also be written as:
e.g: std::string search_path = “C:/Strawberry/”;

search_path can also be specific file extensions:
e.g: std::string search_path = “D:\\Datasets\\images\\*.png”;

The function outputs two vectors (std::vector<std::string>): for file names and folder names.

Full usage example for dir_ff function:

int main(int argc, char* argv[])
{

vector<string> fnames;
vector<string> foldernames;

//dir_ff(“D:/Datasets/images/*.png”, fnames, foldernames);
dir_ff(“D:\\Datasets\\images\\*.png”, fnames, foldernames);
//dir_ff(“C:\\Strawberry\\*.txt”, fnames, foldernames);

cout << “num of files found = ” << fnames.size() << endl;
cout << “num of folders found = ” << foldernames.size() << endl;

cout << “============ Files ==============” << endl;
for (int i = 0; i < fnames.size(); i++)
cout << fnames[i] << endl;

cout << “============ Folders ==============” << endl;
for (int i = 0; i < foldernames.size(); i++)
cout << foldernames[i] << endl;

return 0;
}

Example usage:

Imagine that in a given folder “C:/Users/Kyaw/Desktop/test_fold”, there are the following 2 image files, 3 text files, 1 cpp file and 1 folder as follows:

cpan
DISTRIBUTIONS.txt
I00000.png
I00001.png
image processing in opencv.cpp
README.txt
relocation.txt

The following code:

vector<string> fnames;
vector<string> foldernames;
dir_ff(“C:/Users/Kyaw/Desktop/test_fold/”, fnames, foldernames);

cout << “num of files found = ” << fnames.size() << endl;
cout << “num of folders found = ” << foldernames.size() << endl;

cout << “============ Files ==============” << endl;
for (int i = 0; i < fnames.size(); i++)
cout << fnames[i] << endl;

cout << “============ Folders ==============” << endl;
for (int i = 0; i < foldernames.size(); i++)
cout << foldernames[i] << endl;

will output:

num of files found = 0
num of folders found = 0
============ Files ==============
============ Folders ==============
Press any key to continue . . .

This is to be expected as the format is wrong. It should be:

vector<string> fnames;
vector<string> foldernames;
dir_ff(“C:/Users/Kyaw/Desktop/test_fold/*.*”, fnames, foldernames);

cout << “num of files found = ” << fnames.size() << endl;
cout << “num of folders found = ” << foldernames.size() << endl;

cout << “============ Files ==============” << endl;
for (int i = 0; i < fnames.size(); i++)
cout << fnames[i] << endl;

cout << “============ Folders ==============” << endl;
for (int i = 0; i < foldernames.size(); i++)
cout << foldernames[i] << endl;

which will output:

num of files found = 6
num of folders found = 3
============ Files ==============
DISTRIBUTIONS.txt
I00000.png
I00001.png
image processing in opencv.cpp
README.txt
relocation.txt
============ Folders ==============
.
..
cpan
Press any key to continue . . .

Escaped Backslash can also be used:

dir_ff(“C:\\Users\\Kyaw\\Desktop\\test_fold\\*.*”, fnames, foldernames);

which will output the same output as above.

Only files with a specific extension can be searched as shown below:

vector<string> fnames;
vector<string> foldernames;
dir_ff(“C:\\Users\\Kyaw\\Desktop\\test_fold\\*.png”, fnames, foldernames);

cout << “num of files found = ” << fnames.size() << endl;
cout << “num of folders found = ” << foldernames.size() << endl;

cout << “============ Files ==============” << endl;
for (int i = 0; i < fnames.size(); i++)
cout << fnames[i] << endl;

cout << “============ Folders ==============” << endl;
for (int i = 0; i < foldernames.size(); i++)
cout << foldernames[i] << endl;

which will output:

num of files found = 2
num of folders found = 0
============ Files ==============
I00000.png
I00001.png
============ Folders ==============
Press any key to continue . . .

======================================================
(2) dir_fnames function
======================================================
This function is a convenient wrapper over dir_ff so that files with multiple specific extensions can be looked for. Moreover, it will return many useful other things such as fullpaths and file names with extensions. This will very useful in reading/saving batches of files including images. Therefore, this function has the same functionality as a matlab function “dir_cell” that I’ve written before.

Get the filenames with specific extensions, their full paths, file names with extensions removed in a given directory.

Usage example:

Imagine that in a given folder “C:/Users/Kyaw/Desktop/test_fold”, there are the following 2 image files, 3 text files, 1 cpp file and 1 folder as follows:

cpan
DISTRIBUTIONS.txt
I00000.png
I00001.png
image processing in opencv.cpp
README.txt
relocation.txt

The following code can be written:

string dir_given = “C:/Users/Kyaw/Desktop/test_fold/”;
//string dir_given = “C:\\Users\\Kyaw\\Desktop\\test_fold\\”;
std::vector<std::string> str_exts;
str_exts.push_back(“*.png”); //png files
str_exts.push_back(“*.txt”); // txt files
std::vector<std::string> fnames;
std::vector<std::string> fnames_fullpath;
std::vector<std::string> fnames_noext;
dir_fnames(dir_given, str_exts, fnames_fullpath, fnames, fnames_noext);

for (int i = 0; i < fnames.size(); i++)
{
cout << fnames[i] << endl;
cout << fnames_fullpath[i] << endl;
cout << fnames_noext[i] << endl;
cout << “====================” << endl;
}

==================

This will output:

I00000.png
C:/Users/Kyaw/Desktop/test_fold/I00000.png
I00000
====================
I00001.png
C:/Users/Kyaw/Desktop/test_fold/I00001.png
I00001
====================
DISTRIBUTIONS.txt
C:/Users/Kyaw/Desktop/test_fold/DISTRIBUTIONS.txt
DISTRIBUTIONS
====================
README.txt
C:/Users/Kyaw/Desktop/test_fold/README.txt
README
====================
relocation.txt
C:/Users/Kyaw/Desktop/test_fold/relocation.txt
relocation
====================
Press any key to continue . . .

The following code:

string dir_given = “C:/Users/Kyaw/Desktop/test_fold/”;
//string dir_given = “C:\\Users\\Kyaw\\Desktop\\test_fold\\”;
std::vector<std::string> str_exts;
str_exts.push_back(“*.png”);
std::vector<std::string> fnames;
std::vector<std::string> fnames_fullpath;
std::vector<std::string> fnames_noext;
dir_fnames(dir_given, str_exts, fnames_fullpath, fnames, fnames_noext);

for (int i = 0; i < fnames.size(); i++)
{
cout << fnames[i] << endl;
cout << fnames_fullpath[i] << endl;
cout << fnames_noext[i] << endl;
cout << “====================” << endl;
}

=========================

… will output (since only looking for png files:

I00000.png
C:/Users/Kyaw/Desktop/test_fold/I00000.png
I00000
====================
I00001.png
C:/Users/Kyaw/Desktop/test_fold/I00001.png
I00001
====================
Press any key to continue . . .

The following code:

string dir_given = “C:/Users/Kyaw/Desktop/test_fold/”;
//string dir_given = “C:\\Users\\Kyaw\\Desktop\\test_fold\\”;
std::vector<std::string> str_exts;
str_exts.push_back(“*.png”);
std::vector<std::string> fnames;
std::vector<std::string> fnames_fullpath;
std::vector<std::string> fnames_noext;
dir_fnames(dir_given, str_exts, fnames_fullpath, fnames, fnames_noext);

for (int i = 0; i < fnames.size(); i++)
{
cout << fnames[i] << endl;
cout << fnames_fullpath[i] << endl;
cout << fnames_noext[i] << endl;
cout << “====================” << endl;
}

=========================

… will output:

I00000.png
C:/Users/Kyaw/Desktop/test_fold/I00000.png
I00000
====================
I00001.png
C:/Users/Kyaw/Desktop/test_fold/I00001.png
I00001
====================
DISTRIBUTIONS.txt
C:/Users/Kyaw/Desktop/test_fold/DISTRIBUTIONS.txt
DISTRIBUTIONS
====================
README.txt
C:/Users/Kyaw/Desktop/test_fold/README.txt
README
====================
relocation.txt
C:/Users/Kyaw/Desktop/test_fold/relocation.txt
relocation
====================
image processing in opencv.cpp
C:/Users/Kyaw/Desktop/test_fold/image processing in opencv.cpp
image processing in opencv
====================
Press any key to continue . . .


The code is available at: https://github.com/Kyaw-Kyaw-Htike/file-IO-utility-library-for-Cpp

  • Programming languages: C++