c ++ commonly used functions

 

#include <the iostream>   
#include <the io.h> 
#include <direct.h> 
#include <time.h> 
#include <String> 
#include <Vector> 

the using namespace STD; 

// read a given path and all folder with a file name, and full path 
void getAllFilesInDirs (String path, Vector <String> & fileFullpath) { 

	_finddata_t the fileInfo; 
	String S; 
	const char * filePath = s.assign (path) .append ( "\\ * *. ") .c_str (); 
	// convert the string to const char * type 
	intptr_t fileHandle = _findfirst (filePath, & the fileInfo); 
	// reads the first information file handle file type is long will not be compatible [Win10 platform with long statement file handle Crash] 
	String F; 
	IF (fILEHANDLE == -1) { 
		COUT << "error \ n-"; 
		return;
	}
	while (_findnext(fileHandle, &fileInfo) == 0) {

		if (fileInfo.attrib & _A_SUBDIR) {
			if (strcmp(fileInfo.name, ".") == 0 || strcmp(fileInfo.name, "..") == 0)  continue;
			string newPath = path + "\\" + fileInfo.name;
			getAllFilesInDirs(newPath, fileFullpath);
		}
		else {
			f = path + string("\\") + string(fileInfo.name);
			fileFullpath.push_back(f);
		}
	};
	_findclose(fileHandle);
}


////读取某给定路径下后缀名为format得文件名称,并带完整路径
void getAllFilesByformat(string path, vector<string>& fileFullpath, string format) {
	_finddata_t fileInfo;
	string s;
	const char* filePath = s.assign(path).append("\\*").append(format).c_str();
	// the string into a const char * type
	FILEHANDLE = _findfirst The intptr_t (filePath, & the fileInfo); 
	// reading the first file information file handle type long appears incompatible [Win10 internet with long declaration file handle Crash] 
	String F; 
	IF (FILEHANDLE == -1) { 
		COUT << "error \ n-"; 
		return; 
	} 
	the while (_findnext (FILEHANDLE, & the fileInfo) == 0) { 

		IF (fileInfo.attrib & _A_SUBDIR) { 
		} 
		the else { 
			F + = path String ( "\\") + String (fileInfo.name); 
			fileFullpath.push_back (F); 
		} 
	}; 
	_findclose (FILEHANDLE); 
} 

// get the file extension 
String GetExtension (String filename) { 
	return filename.substr (filename.find_last_of () + '.' 1); 
} 

// string divided into vector
Vector <string> string_split (const string STR &, const string & the delim) { 
	Vector <string> RES; 
	IF ( "" == STR) RES return; 
	// first string is to be cut from a string type to convert char * type   
	char * strs = new char [str.length () + 1]; // Do not forget   
	strcpy (STRs, str.c_str ()); 

	char * D = new new char [delim.length () +. 1]; 
	strcpy (D, delim.c_str ()); 

	char * P = strtok (STRs, D); 
	the while (P) { 
		string S = P; // converts a string obtained by dividing a string type   
		res.push_back (s); // stored The resulting array   
		P = strtok (NULL, D); 
	} 

	return RES; 
} 


// Create a folder recursively 
int self_mkdirs (String filename) { 
	filename + = "\\"; 
	char * fileName = (char *) filename.c_str () ; 
	char * Tag;
	A 0 = int;
	for (Tag = fileName; * Tag; Tag ++, A ++) 
	{ 
		IF (* Tag == '\\') 
		{ 
			String new_path filename.substr = (0, + A. 1); 

			IF (_access (new_path.c_str (), 0) == -1) // If the folder does not exist 
				_mkdir (new_path.c_str ()); // create 
		} 
	} 

	return 0; 
}

  

Guess you like

Origin www.cnblogs.com/dxscode/p/11698344.html