CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2006
    Posts
    46

    [RESOLVED] Reading multiple files in a folder

    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.

    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 );
    
    }
    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Reading multiple files in a folder

    I suggest reading this FAQ.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2006
    Posts
    46

    Re: Reading multiple files in a folder

    Quote Originally Posted by cilu
    I suggest reading this FAQ.
    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...........

  4. #4
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: Reading multiple files in a folder

    Quote Originally Posted by renuka75
    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
    PHP Code:
     std::vector<std::stringvecDatFiles;
     
    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.
    Last edited by kumaresh_ana; June 12th, 2007 at 01:56 AM.

  5. #5
    Join Date
    Nov 2006
    Posts
    46

    Re: Reading multiple files in a folder

    Quote Originally Posted by kumaresh_ana
    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
    PHP Code:
     std::vector<std::stringvecDatFiles;
     
    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!!!!!!

  6. #6
    Join Date
    Nov 2006
    Posts
    46

    Re: Reading multiple files in a folder

    Hi All,
    I tried to use the following code (from FAQ) as follows to read files.
    Code:
    #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!

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Reading multiple files in a folder

    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.

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

  8. #8
    Join Date
    Nov 2006
    Posts
    46

    Re: Reading multiple files in a folder

    Quote Originally Posted by Philip Nicoletti
    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.

    Code:
    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 ......
    Code:
    while(in>>tmp)  
    { 
       cout << tmp <<endl;
    }
    in.clear();  // clear internal flags
    in.close();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured