+ 4
i am trying to use dirent.h to check whether a file is present in a directory but it is not returning anything
void exist(std::string location) { // if error occurs false will be returned bool exists = false; DIR *dir = NULL; dirent *entry; try { if(dir==NULL) dir = opendir(location.c_str()); while((entry = readdir(dir))!=NULL) { if(strcmp(location.c_str(),entry->d_name)) exists = true; } }catch(int x) { std::cout<<"error"<<std::endl; } std::cout<<exists<<std::endl; } is something wrong in the code which checks whether files is present in directory or not?
2 Answers
+ 2
why you not try to open file normally. if the file is doesnt exist, it will throw an error
- 1
write in correct format