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
LVL 1
dedsiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("*.txt"));
while (bWorking)
{
    bWorking = finder.FindNextFile();

    try
    {
        CStdioFile txtFile(finder.GetFileName(), CFile::modeRead | CFile::typeText);

        // Read the file
        // txtFile.ReadString();
    }
    catch (CFileException *pEx)
    {
        // Error handling...

        pEx->Delete();
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjswartCommented:
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>
0
dedsiAuthor Commented:
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.
0
dedsiAuthor Commented:
btw, I found it out by myself.

Thank you both for your answers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.