Cannot #include <dirent.h> in Visual Studio 2005
When I include dirent.h
(#include <dirent.h>
) in Visual Studio 2005, I get the error:
fatal error C1083: Cannot open include file: 'dirent.h': No such file or direc开发者_如何学JAVAtory
I am new to C++; can anyone please provide me with the solution for this error?
The file dirent.h is not a C++ Standard header file. As you are on Windows, you probably want to use the FindFirstFile and related functions, declared in windows.h
The error message says it all. The file does not exist or it is not in the correct directory. Check out this website. It includes a free implementation of dirent.h
.
You should add the directory where the file is located to the "additional include folders" in the Visual Studio project properties.
Try including just dir.h
and if that doesn't work try io.h
#include <errno.h>
#include <iostream>
#include <io.h>
#include <time.h>
using namespace std;
bool canDelete(int timeCreate);
int main() {
struct _finddata_t data;
int handle;
handle = _findfirst("test.txt", &data);
if(handle == -1) {
exit(1);
}
if(canDelete(data.time_create)) {
cout << "Deleting file ...\n\n";
} else {
cout << "File ok.\n\n";
}
}
/**
* @param: the time in seconds that the file was created.
* @return: true if the file was created more than 7 days,
* false otherwise.
**/
bool canDelete(int time_create) {
time_t seconds = time(NULL);
int days = 7;
int max_time = 60 * 60 * 24 * days;
int time_passed = seconds - time_create;
if(time_passed > max_time) {
return true;
} else {
return false;
}
}
精彩评论