RE: [Dev-C++] trying to open a specific folder
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Reid T. <Rei...@at...> - 2005-09-16 15:15:50
|
Patel, Nilesh M wrote: > Curious to know if someone knows how to open a folder (abc > folder) and then search for a particular file (xyz.txt file) in C++. >=20 > I do know how use the I/O functionality for files in C++, but > I am having difficulty with opening a folder. Any help would > be most appreciated. Thanks. >=20 > from http://www.informit.com/guides/content.asp?g=3Dcplusplus&seqNum=3D245&rl=3D= 1 Standard C++ doesn't have a library for manipulating directories. The closest thing you can get is Boost's filesystem library. However, most implementations nowadays support the quasi-standard <dirent.h> and <dir.h> libraries. These libraries, originally written in C, enable you to list the files in a directory, change the current directory, create a new directory and delete an existing directory. #include <errno.h> #include <dirent.h> #include <stdio.h> #include <stdlib.h> int main() { DIR *pdir; struct dirent *pent; pdir=3Dopendir("."); //"." refers to the current dir if (!pdir){ printf ("opendir() failure; terminating"); exit(1); } errno=3D0; while ((pent=3Dreaddir(pdir))){ printf("%s", pent->d_name); } if (errno){ printf ("readdir() failure; terminating"); exit(1); } closedir(pdir); } reid |