Click to See Complete Forum and Search --> : [RESOLVED] Reading multiple files in a folder


renuka75
June 11th, 2007, 12:40 AM
Hi All,
I have several files like name as follows,

res0001847.dat
res0001850.dat
res0001854.dat
res0001860.dat

Names are something like above which has numerical analysis data. So far I used following coding to read above data files in Linux OS.


#include <dirent.h>
#include <cerrno>

int readFiles(char * dirname)
{
DIR* dirp;
string buff;
struct dirent* dp;
dirp = opendir(dirname);

if ( !dirp ) {
cout << "Error: failure opening directory" << endl;
exit(1);
}

errno = 0;

ofstream output;
output.open("driftdata", ios::out);

while (( dp = readdir(dirp) )!=NULL) { // read directory

if(strstr(dp->d_name, ".dat")) { // to check dat files

ifstream input(dp->d_name); // to read files
int lineno=0;
while( getline(input, buff) ) { output << buff << endl; }

if ( errno ){
cout << "Error: readdir() failure!" << endl;
exit(1);
}
}
output.close();
closedir( dirp );

}


But now my problem is, I should work above task in a Windows OS (MS Visual Studio). How can I do?
Please help me!!!!!!!!!

Rgds,
Renuka.

cilu
June 11th, 2007, 06:26 AM
I suggest reading this FAQ (http://www.codeguru.com/forum/showthread.php?t=312461).

renuka75
June 11th, 2007, 10:19 PM
I suggest reading this FAQ (http://www.codeguru.com/forum/showthread.php?t=312461).

Yeah I have read FAQ well.......
I dont have a hint to work above job without <dirrent.h> header file.

I know if files names are named in a sequence like 1.......dat, 2........dat, 3.....dat. But in my case files names dont have have any sequence as well.

If you know any hint to do this Pls tell me...........

kumaresh_ana
June 12th, 2007, 01:50 AM
Yeah I have read FAQ well.......
The FAQ is clear what you should do. You don't need dirrent.h instead use the SearchDirectory function listed in the FAQ like this

std::vector<std::string> vecDatFiles;
SearchDirectory(vecDatFiles, "c:\datfiles", "dat", false);
....

Now vecDatFiles will hold the full path of the files needed. Now open every file in the vector and do the operations needed.

renuka75
June 12th, 2007, 02:29 AM
The FAQ is clear what you should do. You don't need dirrent.h instead use the SearchDirectory function listed in the FAQ like this

std::vector<std::string> vecDatFiles;
SearchDirectory(vecDatFiles, "c:\datfiles", "dat", false);
....

Now vecDatFiles will hold the full path of the files needed. Now open every file in the vector and do the operations needed.

Thank you very much......... Now I got the idea, I will try based on that code. If I need any help I will post here!!!!!!

renuka75
June 12th, 2007, 06:50 AM
Hi All,
I tried to use the following code (from FAQ) as follows to read files.

#include <string>
#include <vector>
#include <iostream>
#inlcude <fstream>

#include <windows.h>
#include <conio.h>



int SearchDirectory(std::vector<std::string> &refvecFiles,
const std::string &refcstrRootDirectory,
const std::string &refcstrExtension,
bool bSearchSubdirectories = true)
{
std::string strFilePath; // Filepath
std::string strPattern; // Pattern
std::string strExtension; // Extension
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information


strPattern = refcstrRootDirectory + "\\*.*";

hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0] != '.')
{
strFilePath.erase();
strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(bSearchSubdirectories)
{
// Search subdirectory
int iRC = SearchDirectory(refvecFiles,
strFilePath,
refcstrExtension,
bSearchSubdirectories);
if(iRC)
return iRC;
}
}
else
{
// Check extension
strExtension = FileInformation.cFileName;
strExtension = strExtension.substr(strExtension.rfind(".") + 1);

if(strExtension == refcstrExtension)
{
// Save filename
refvecFiles.push_back(strFilePath);
}
}
}
} while(::FindNextFile(hFile, &FileInformation) == TRUE);

// Close handle
::FindClose(hFile);

DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
return dwError;
}

return 0;
}


int main()
{

string Dir;
fstream in;
char tmp[1024];
int iRC = 0;
std::vector<std::string> vecDatFiles;

// Search 'c:' for 'dat' files including subdirectories
iRC = SearchDirectory(vecAviFiles, "c:", "dat");
if(iRC)
{
std::cout << "Error " << iRC << std::endl;
return -1;
}

// Print results
for(std::vector<std::string>::iterator iterDat = vecDatFiles.begin();
iterDat != vecDatFiles.end();
++iterDat) {
std::cout << *iterDat << std::endl;

//------Here I used my codes to read each files
Dir=*iterDat;
in.open(Dir.c_str(), ios::in);
while(in>>tmp) { cout << tmp <<endl;}

return 0;
}


My code in red color only read first file. Please tell me how should I code to read all files in a folder.

Thank you very much!

Philip Nicoletti
June 12th, 2007, 07:39 AM
1) Your code doesn't compile, so I can only guess ...

2) you are re-using the same fstream object to read multiple
files. After you loop and read thru the first file, the stream's
state becomes false. You need to clear the streams internal
flags (see below).

3) It is easier to declare the fstream variable with-in your
for loop. That way you do not need to worry about the
stream's state.


while(in>>tmp)
{
cout << tmp <<endl;
}
in.clear(); // clear internal flags

renuka75
June 12th, 2007, 08:06 AM
1) Your code doesn't compile, so I can only guess ...

2) you are re-using the same fstream object to read multiple
files. After you loop and read thru the first file, the stream's
state becomes false. You need to clear the streams internal
flags (see below).

3) It is easier to declare the fstream variable with-in your
for loop. That way you do not need to worry about the
stream's state.


while(in>>tmp)
{
cout << tmp <<endl;
}
in.clear(); // clear internal flags


Thank you very much for your help. Now it works with another important file closing syntex ......

while(in>>tmp)
{
cout << tmp <<endl;
}
in.clear(); // clear internal flags
in.close();