Link to home
Start Free TrialLog in
Avatar of dedsi
dedsiFlag for Sweden

asked on

Reading text-files in a directory

I want to make an application that will read all text files (*.txt) in the current directory and then generate some sort of output. The directory can be whatever directory on the computer and the files can have any possible names. How do I get the names of the files that exists in the current directory and then opens them one at a time for reading?

/dedsi
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjswart
mjswart

chensu's too quick:

struct _tfinddata_t txt_file;
long hFile;

// Find first .txt file in current directory
if( (hFile = _tfindfirst( "*.txt", &txt_file )) == -1L )
   return;
// Do something with the first file txt_file.name

// Find subsequent files
while( _tfindnext( hFile, &rpt_file ) == 0 )
{
   // Do something with the first file txt_file.name      
}
_findclose( hFile );


Doing something probably involves opening and closing:
you can use
ifstream fin("txt_file.name");
and
fin.close();

don't forget to #include <fstream.h> and<shlobj.h>
Avatar of dedsi

ASKER

chensu,

how do I do to read the whole file? The thing you suggested (txtFile.ReadString()) only reads one line but the files can have multiple lines.
Avatar of dedsi

ASKER

btw, I found it out by myself.

Thank you both for your answers!