+ 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?

12th Feb 2018, 11:56 AM
shobhit
shobhit - avatar
2 ответов
+ 2
why you not try to open file normally. if the file is doesnt exist, it will throw an error
13th Feb 2018, 5:57 AM
Kevin AS
Kevin AS - avatar
- 1
write in correct format
2nd Mar 2018, 11:05 AM
GAUTAM KUMAR
GAUTAM KUMAR - avatar