简体   繁体   中英

Listing the Files in a Directory WinApi

In directories I have only 3 files (poem.txt , mac. txt , some_file.txt).

Why in my output 2 weird files with name "." and ".." ?

What is that?

#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

int main()
{
    WIN32_FIND_DATA fileData;
    HANDLE hFind = FindFirstFile(L"D:\\Univesity\\PROJECTS\\OS_Course\\poem_old\\*", &fileData);

    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            //wcout << fileData.cFileName << endl;
            WIN32_FIND_DATA compared_fileData;
            HANDLE compared_hFind = FindFirstFile(L"D:\\Univesity\\PROJECTS\\OS_Course\\poem2_new\\*", &compared_fileData);

            if (compared_hFind != INVALID_HANDLE_VALUE) {
                do {
                    //wcout << (wstring) fileData.cFileName << endl << (wstring) compared_fileData.cFileName;
                    if ( (wstring) fileData.cFileName == (wstring) compared_fileData.cFileName) {
                        FILETIME fileWriteTime = fileData.ftLastWriteTime;
                        FILETIME compared_fileWriteTime = compared_fileData.ftLastWriteTime;
                        /*
                            -1  First file time is earlier than second file time. 
                            0   First file time is equal to second file time. 
                            1   First file time is later than second file time. 
                        */
                        int comparationResult = CompareFileTime(&fileWriteTime, &compared_fileWriteTime);
                        if (comparationResult == 1) {
                            wcout << "Comparation: " << (wstring)fileData.cFileName << " is later than " << (wstring)compared_fileData.cFileName << endl;
                        }
                        else if (comparationResult == -1) {
                            wcout << "Comparation: " << (wstring)fileData.cFileName << " is earlier than " << (wstring)compared_fileData.cFileName << endl;
                        }
                    }
                } while (FindNextFile(compared_hFind, &compared_fileData));
            }
        } while (FindNextFile(hFind, &fileData));
        FindClose(hFind);
    }
}

Output:

Comparation: . is later than .

Comparation: .. is later than ..

Comparation: black.txt is earlier than black.txt

Comparation: mac.txt is earlier than mac.txt

Comparation: some_file.txt is later than some_file.txt

. and .. are pseudo directories. The semantics are documented :

  • Use a period as a directory component in a path to represent the current directory, for example ".\\temp.txt". For more information, see Paths .
  • Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory, for example "..\\temp.txt". For more information, see Paths .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM