dedsi
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
/dedsi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
btw, I found it out by myself.
Thank you both for your answers!
Thank you both for your answers!
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>