?
Solved

Reading text-files in a directory

Posted on 1999-12-08
4
Medium Priority
?
164 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:dedsi
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 800 total points
ID: 2267037
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
 
LVL 1

Expert Comment

by:mjswart
ID: 2267050
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
 
LVL 1

Author Comment

by:dedsi
ID: 2268182
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
 
LVL 1

Author Comment

by:dedsi
ID: 2268644
btw, I found it out by myself.

Thank you both for your answers!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question