3

Here is the program:

#include <stdio.h>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    const char c[] = "C:\\Users\\*.*";

    hFind = FindFirstFile(c, &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        printf("FindFirstFile failed (%d)\n", GetLastError());
        return 1;
    }
    else
    {
        cout << "The first file found is " << FindFileData.cFileName << endl;
    }

    for (int i = 0; i < 12; i++)
    {
        if (!FindNextFile(hFind, &FindFileData))
        {
            printf("FindNextFile failed (%d)\n", GetLastError());
        }
        else
        {
            cout << "The next file found is " << FindFileData.cFileName << endl;
        }
    }

    FindClose(hFind);
    return 0;

}

Prints this:

enter image description here

No matter what directory I goto, it always prints the first two lines with file . and .. ? why is that?

quantum231
  • 2,420
  • 3
  • 31
  • 53
  • @user4581301 Perhaps not quite, but they do have the same meaning in Windows as in Linux. – user253751 Jun 23 '16 at 22:50
  • 2
    Check the accepted answer here http://stackoverflow.com/questions/2700750/problems-with-searching-in-files-and-directories-windows-programming – Jim Hewitt Jun 23 '16 at 22:51
  • Yeah, I duped it it, then tried to edit the comment before anyone noticed. – user4581301 Jun 23 '16 at 22:51
  • Pretty much every OS since we got hierarchical storage has had those two. Additionally in DOS/Windows there are a bunch of device files like `prn` `nul` `con`, but I guess it's only us crusty old folks that remember the joy of using copy con to create configuration files before you could just fire up notepad. – theB Jun 23 '16 at 22:56
  • OK perfect, my question is answered. – quantum231 Jun 23 '16 at 22:56
  • Yeah, I remember the good ol' days... 40 miles... uphill both ways... – user4581301 Jun 23 '16 at 22:57

2 Answers2

5

All directories have these directories in them.

The '.' and '..' have special meaning ... if you don't need them, simply ignore.

Example: from Linux

dmn@DM5:~$ ls -lsa

total 3144
  4 drwxr-xr-x 87 dmn dmn        4096 Jun 23 09:43 .
  4 drwxr-xr-x  5 root  root     4096 Jan  3 08:43 ..
  .... and lots more strings
2785528
  • 5,438
  • 2
  • 18
  • 20
  • I really did not know that (electronic engineer) – quantum231 Jun 23 '16 at 22:52
  • 1
    Quite some time ago, I'm sure I did not know this either. I have a BSEE and hw design experience, but mostly embedded software. This has been a humbling site to try to help people. Good luck. – 2785528 Jun 23 '16 at 22:58
3

These are special linux link-like files that represent current and parent directory. Refer to this answer for more information.

Community
  • 1
  • 1
Jezor
  • 3,253
  • 2
  • 19
  • 43
  • They are not "Linux" files. Windows inherited this from DOS, which was doing it long before Windows. DOS probably got it from CP/M, but I can't remember that far back. – Cody Gray - on strike Jun 24 '16 at 17:30