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.
Asked
Active
Viewed 1,977 times
1

Community
- 1
- 1

Freddykong
- 95
- 12
-
you want to learn the difference between *header files* and *libraries*. Downloading the header will not suffice. – tofro Aug 18 '16 at 07:35
-
Can you explain more please? I mean I followed the little tutorial and I could use function from dirent.h and those functions actually work :/ – Freddykong Aug 18 '16 at 07:39
-
It is header only library, so nothing but an .h file is needed. It seems `seekdir` is simply not implemented by the author. – Ari0nhh Aug 18 '16 at 07:42
-
@Ari0nhh I was looking for other implementation too and it seems that seekdir is just not implemented :/ – Freddykong Aug 18 '16 at 07:46
-
Windows POSIX support is hit and miss. [Give this a try instead.](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx) – user4581301 Aug 18 '16 at 08:41
-
@user4581301 Sorry I forgot to mention that I wanted to avoid "Windows version" ( Of course it would be easier since I'm on VS but I was like "oh $*ù^$ this I want to use linux things" ( I learnt C and C++ programming on Linux platform :p ). But thank you anyway! :) – Freddykong Aug 18 '16 at 08:56
-
1If your compiler is up to date, you may be able to turn on C++17's file system support by setting a compiler option to get experimental, pre standardization libraries. other than that, it's a portability library like the already-rejected boost. – user4581301 Aug 18 '16 at 09:10
-
@user4581301 Ok i'll give a try! thanks ! – Freddykong Aug 18 '16 at 11:16
2 Answers
0
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.

Peter K
- 1,372
- 8
- 24
0
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;
}

user4581301
- 33,082
- 7
- 33
- 54

Software_Designer
- 8,490
- 3
- 24
- 28
-
WRT "which requires the use of wide characters[,]" Windows typically provides two functions, one with a 'W' suffix for wide characters and one with an 'A' suffix for good ol' 8 bit ANSI.By default, a given function will be defined to point at the 'W' version. Eg. a `FindFirstFile` is defined to `FindFirstFileW`. `FindFirstFileA` can be called directly or somewhere out there there will be a compiler option to globally redirect from the 'W's to the 'A's. Danged if I can remember the option, though. – user4581301 Aug 18 '16 at 18:23