简体   繁体   中英

Cannot find seekdir() in dirent.h for Windows (C++)


I'm Actually working on a project and I have to browse directories and for this i'm using dirent.h library since I dont want to use Boost just for that.
Hence I found this post <dirent.h> in visual studio 2010 or 2008 which lead to here http://www.softagalleria.net/dirent.php where I downloaded and installed dirent.h.
So dirent.h is installed and I have no problem using basic functions like opendir, readdir but when I wanted to use seekdir() function, it seems that it does not exist in the library so I went in dirent.h to verify my hypothesis and (Thanks Ctrl+F) seekdir is indeed missing.
Did I miss something or do I have to find a trick to get this function ...?
Thanking you.

The only functions available in that header are:

DIR *opendir (const char *dirname);
struct dirent *readdir (DIR *dirp);
int closedir (DIR *dirp);
void rewinddir (DIR* dirp);

There is no trick to get the functionality you need. You simply need to find another library for this.

在此处输入图片说明

If you cant locate the header file dirent.h then try using WIN32_FIND_DATA , FindFirstFile() and FindNextFile() as an alternative. Two different codes are submitted. One for Visual Studio 6.o and another for Visual Studio 2013 which requires the use of wide characters.

Code for Visual studio 6.0:

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

using namespace std;


void listdirandfiles(string dir){
    HANDLE hFind;
    WIN32_FIND_DATAA data;

    hFind = FindFirstFileA(dir.c_str(), &data);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            printf("%s\n", data.cFileName);
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }
}

int main(int argc, char** argv){

        string dir = "c:\\*.*";
        cout<<"\nListing directories or files..\n\n";
        listdirandfiles(dir);

        cout<<"\nPress ANY key to close.\n\n";
        cin.ignore(); cin.get();
return 0;
}

code for Visual Studio 2013 :

// visual studio 2013
// listdirConsoleApplication15.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;


wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
    wchar_t* wString = new wchar_t[4096];
    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
    return wString;
}

void listdirandfiles(char *wstr){
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    hFind = FindFirstFile(convertCharArrayToLPCWSTR(wstr), &FindFileData);

    do{
        _tprintf(TEXT("%s\n"), FindFileData.cFileName);
    } while (FindNextFile(hFind, &FindFileData));

    FindClose(hFind);

}

int main( )
{

    char *wstr = "c:\\*.*";

    cout << "\nListing directories or files..\n\n";

    listdirandfiles(wstr);

    cout << "\nPress ANY key to close.\n\n";
    cin.ignore(); cin.get();

    return 0;
}

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