GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jun-2007, 00:17
sitha sitha is offline
Junior Member
 
Join Date: Jul 2006
Posts: 31
sitha is on a distinguished road

Reading multiple files in a folder


Hi All,
I used follwong code in linux OS to read multiple files in a folder with names like follwos,
res00012.dat
res00019.dat
res00024.dat

No sequence of number in file names

CPP / C++ / C Code:
#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 );

}

Now I want to use similar to code to read data from files in MS Visual Studio. But VS didn't support for dirent.h header file. Without that header file, How can I do this job?
Please give me some hints!!!!!

Rgds,
Sitha.
Last edited by LuciWiz : 12-Jun-2007 at 05:31. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 12-Jun-2007, 20:37
sitha sitha is offline
Junior Member
 
Join Date: Jul 2006
Posts: 31
sitha is on a distinguished road

Re: Reading multiple files in a folder


I solved this problem!

Thank you.
  #3  
Old 13-Jun-2007, 02:11
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 349
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Reading multiple files in a folder


Could you also post the solution here, so this thread might be useful to someone in the future, maybe?
  #4  
Old 13-Jun-2007, 04:13
sitha sitha is offline
Junior Member
 
Join Date: Jul 2006
Posts: 31
sitha is on a distinguished road

Re: Reading multiple files in a folder


Thanks for your idea. This is the code.....

CPP / C++ / C Code:
#include  <string>
#include  <vector>
#include  <iostream>
#include <fstream>

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

using namespace std;


int SearchDirectory(vector<string>		&refvecFiles,
                    const string        &refcstrRootDirectory,
                    const string        &refcstrExtension,
                    bool                bSearchSubdirectories = true)
{
  string		  strFilePath;             // Filepath
  string		  strPattern;              // Pattern  
  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()
{
  int            iRC         = 0;
  vector<string> vecDatFiles;
  string		 buff, Dir;
  fstream		 input;


  // Search 'c:' for '.dat' files including subdirectories
  iRC = SearchDirectory(vecDatFiles, "C:\\Program Files\\cygwin\\new_wrapper\\BuildingData", "dat");
  if(iRC)
  {
    cout << "Error " << iRC << std::endl;
    return -1;
  }

  // Print results
  for(vector<string>::iterator iterDat = vecDatFiles.begin();
      iterDat != vecDatFiles.end(); ++iterDat){
		  Dir=*iterDat;
		  input.open(Dir.c_str(), ios::in);
		  while(getline( input, buff)){
			  cout << buff<<endl; }
		  cout << *iterDat << std::endl;
                            input.close();
                            input.clear();
}
  
  return 0;
}
  #5  
Old 13-Jun-2007, 04:25
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 858
admin will become famous soon enough

Re: Reading multiple files in a folder


Thank you for sharing the information, Sitha.

Next time, when you have code examples to include in your message, you can post them like this:

[cpp]
// your code example here ...
[/cpp]

so your code is syntax highlighted.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make columns when reading in files??? jtrink C++ Forum 1 02-May-2007 14:28
C++ for moving files across network? dlp C++ Forum 1 25-May-2006 12:22
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
Need help reading data from files blitzy C Programming Language 5 15-Mar-2005 19:07
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 14-Jul-2004 23:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:48.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.