int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );
int _chdir(const char *path);
头文件:direct.h
参 数:Path 必选。Path 可能包含驱动器。如果未指定驱动器,则当前驱动器上的默认目录或文件夹。
int _findclose( intptr_t handle );
/*关闭查找*/
Parameters
handle Search handle returned by a previous call to _findfirst. Return ValueIf successful, _findclose returns 0. Otherwise, it returns –1 and setserrno to ENOENT, indicating that no more matching files could be found.
_findclose | <io.h> |
#include "io.h"
#include "direct.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
#include <string>
using std::string;
int main()
{
string dir;
cout << "Input the name of directory: ";
cin >> dir;
if (_access(dir.c_str(), 06) == -1)
{
cerr << "error: directory does not exist." << endl;
exit(-1);
}
if (dir.at(dir.length() - 1) != '\\')
{
dir += '\\';
}
if (_chdir(dir.c_str()) != 0)
{
cerr << "error: function _chdir() failed.";
exit(-1);
}
_finddata_t fileinfo;
memset(&fileinfo, 0x0, sizeof(fileinfo));
intptr_t iFind = _findfirst("*", &fileinfo);
if (iFind == -1)
{
cerr << "error: function _findfirst failed." << endl;
exit(-1);
}
string filePath(dir + fileinfo.name);
cout << "name: " << filePath << endl;
while (_findnext(iFind, &fileinfo) == 0)
{
filePath = dir + fileinfo.name;
cout << "name: " << filePath << endl;
}
_findclose(iFind);
return 0;
}
上面代码转自:http://blog.youkuaiyun.com/selina6874/article/details/6784697