如何使用“。 "和“ ..“判断一个空文件夹? [英] How to use the “ . " and " .. " to judge an empty folder?

查看:61
本文介绍了如何使用“。 "和“ ..“判断一个空文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上看到了一个程序代码单元。它使用。和..来判断一个空文件夹。但我无法理解。谁可以告诉我原理?



I saw a unit of program code on the internet. It uses " . " and " .. " to judge an empty folder. But I can''t understand it. Who can tell me the principle?

#include<io.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void dfsFolder(string folderPath, ofstream &fout)
{
    _finddata_t FileInfo;
    string strfind = folderPath + "\\*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);

    if (Handle == -1L)
    {
        cerr << "can not match the folder path" << endl;

        exit(-1);

    }

    do{



        if (FileInfo.attrib & _A_SUBDIR)

        {



            if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))   //this sentence!//

            {

                string newPath = folderPath + "\\" + FileInfo.name;

                dfsFolder(newPath, fout);

            }

        }

        else  

        {

            fout << folderPath << "\\" << FileInfo.name  << " ";

        }

    }while (_findnext(Handle, &FileInfo) == 0);



    _findclose(Handle);

    fout.close();

}

推荐答案

在Windows上,每个目录都有。条目。和..即使其中不存在文件。此代码扫描目录并标识作为子目录的每个条目。它必须检查。和..存在但无效用于此目的。
On Windows, every directory has entries for "." and ".." even if no files exist in it. This code scans a directory and identifies every entry that is a subdirectory. It has to check for "." and ".." which exist but are not valid for this purpose.


。和..是文件夹begin中的两个文件!
the "." and ".." are two file in the folder begin!


这篇关于如何使用“。 &QUOT;和“ ..“判断一个空文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆