CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: reading files in a folder

  1. #1
    Join Date
    Jul 2006
    Posts
    45

    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.

  2. #2
    Join Date
    Aug 2002
    Posts
    879

    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

  3. #3
    Join Date
    Jul 2006
    Posts
    45

    Re: reading files in a folder

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

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: reading files in a folder

    The boost filesystem library offers file/directory functions independent of OS and Compiler.

    boost

    Regards,
    Guido
    Last edited by GNiewerth; May 9th, 2007 at 04:15 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)