Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?

XML
#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();
}
Posted
Comments
Ron Beyer 16-May-13 23:15pm    
Can you post the code for the _findnext routine?
enhzflep 16-May-13 23:33pm    
Findfirst and findnext return any file in the directory. Even an 'empty' directory has two entries in it.
1. The "." entry - short-hand for this directory
2. The ".." entry - short-hand for the parent of this directory.

So, the code you've highlighted is checking to make sure that the currently found entry is neither . or .. (it has already passed a check to see if it's a sub-directory)
RHsoul 17-May-13 0:29am    
Thanks,I am clear!
Volynsky Alex 17-May-13 5:06am    
read what function strcmp doing, and all will become clear:
http://msdn.microsoft.com/en-us/library/vstudio/e0z9k731.aspx

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.
 
Share this answer
 
Comments
RHsoul 17-May-13 1:45am    
You mean these code can't traverse every file included sub-folder , isn't it?
If it can't ,how can l attain it? Can you help me?
the "." and ".." are two file in the folder begin!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-13 0:12am    
No, they are not. Please avoid answering if you don't know the answer yourself.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900