我实际上正在做一个项目,我必须浏览目录,为此我正在使用 dirent.h 库,因为我不想仅仅为此使用 Boost。
因此,我在 Visual Studio 2010 或 2008中找到了这篇文章 <dirent.h>,它指向http://www.softagalleria.net/dirent.php,我在此处下载并安装了 dirent.h。
所以安装了dirent.h,使用opendir、readdir等基本函数没有问题,但是当我想使用seekdir()函数时,库中似乎不存在它,所以我进入dirent.h来验证我的假设并且(感谢 Ctrl+F) seekdir 确实丢失了。
我错过了什么还是我必须找到一个技巧来获得这个功能......?
感谢您。
问问题
1558 次
2 回答
0
如果您找不到头文件,请dirent.h
尝试使用WIN32_FIND_DATA
,FindFirstFile()
和FindNextFile()
作为替代方法。提交了两个不同的代码。一个用于 Visual Studio 6.o,另一个用于 Visual Studio 2013,需要使用宽字符。
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;
}
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;
}
于 2016-08-18T16:13:47.783 回答
0
该标头中唯一可用的功能是:
DIR *opendir (const char *dirname);
struct dirent *readdir (DIR *dirp);
int closedir (DIR *dirp);
void rewinddir (DIR* dirp);
获得所需功能没有任何技巧。您只需要为此找到另一个库。
于 2016-08-18T07:51:47.820 回答