c++常用语法

c++统计一个文件夹中文件的数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
long hFile = 0;
int fileCount = 0;
string tempPath = SavePath;
struct _finddata_t fileinfo;
if ((hFile = _findfirst(tempPath.append("*").c_str(), &fileinfo)) != 0)
{
    while ( _findnext(hFile, &fileinfo) == 0)
    {
        if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
            continue;
        if (fileinfo.attrib & _A_SUBDIR)
            fileCount += 1;
    }
}

string字符串分割

1
2
3
4
5
6
7
8
9
10
11
12
string DirName(string imgPath)
{
    char temp[256];
    std::vector<string> tempresult;
    strcpy(temp, imgPath.c_str());
    char *tempstr = strtok(temp, "");
    while (tempstr != NULL){
        tempresult.push_back(string(tempstr));
        tempstr = strtok(NULL, "");
    }
    return tempresult[2];
}