reading files in a folder
Hi All,
I want to read a several (2000) files in a folder. I did as follows,
class for Readdir,
Code:
class Readdir
{
private:
string dirName;
public:
Readdir ( const string& m) { dirName = m; }
~Readdir() {}
void readFiles() const;
};
member function coding,
Code:
#include <dirent.h>
#include <errno.h>
void Readdir::readFiles () const
{
DIR* dirp;
string buff;
struct dirent* dp;
dirp = opendir( dirName.c_str());
if (!dirp)
{
cout<< "Error: failure opening directory! " << endl;
exit(1);
}
errno = 0;
ofstream output;
output.open("driftdata/drift", ios::out);
while((dp = readdir (dirp))!=NULL)
{
if(strstr (dp->d_name, ".dat"))
{
ifstream input(dp->d_name);
int lineno=0;
while(getline(input, buff))
{
if((++lineno%25)==5)
{
if(lineno ==4055) break;
output<<buff<<endl;
}
}
}
if(errno)
{
cout << " Error :readdir () failure" << endl;
exit(1);
}
}
output.close();
closedir (dirp);
}
main,
Code:
int main(void)
{
Readdir myDir(".");
myDir.readFiles();
return 0;
}
In this case, my coding is working in linux OS (gCC compiler). But I want to run the codes in Windows OS ( Visual Studio).
Please tell me how can I do the above task in Windows.
The main problem is <dirent.h> header file is available in windows.
Please suggest me a different way to read files in a folder.
Thanks a lot in advance.
Re: reading files in a folder
Well on Windows systems you can use
FindFirstFile() FindNextFile() here is an example
http://msdn2.microsoft.com/en-us/library/aa365200.aspx
and check-out this in the codeguru faqs
"Windows SDK File System: How to search for files in a directory and subdirectories?"
http://www.codeguru.com/forum/showthread.php?t=312461
or you can use this alternatively
http://www.softagalleria.net/dirent/index.en.html
Re: reading files in a folder
Quote:
Originally Posted by blueday54555
Thanks for your information.
I wonder much whether is there any way available for files reading independent of OS or Compiler. I expect an idea which can work for all compilers.
Please help me..
Re: reading files in a folder
The boost filesystem library offers file/directory functions independent of OS and Compiler.
boost
Regards,
Guido