Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


FindFirstFile, CFile, and CArchive

Posted on 2003-03-13
Medium Priority
Last Modified: 2013-11-20
Within my CDocument's serialization class, I call the below function that tries to open up all files with the .asm extension in a certain directory and read each line one at a time.  Since this is called from my application's serialization function in the CDocument class, this function is only invoked when

The problem I am having is that on the while(asmArchive.ReadString(strOneLine)) line a message box (looks like and AfxMessageBox) pops up indicating that the file I selected by going to File->open can not
opened. This function, however, shouldn't touch the file that I selected by going to File->open.  This function is simply invoked when the user takes that action.

So, can anyone see what I am doing wrong?  I am creating the CFile and CArchive classes correctly?  Is there a problem since a file handle already exists for the file before I create the CFile and CArchive objects? How about the use of FindFirstFile and FindNext file in the attempt to search a directory.

Or is the problem have to do with the fact that I am creating another CArchive object (for reading files in the selected dir) while handling the CArchive object that was created when the user selects File->open?

CStringList* Casm::getASMLabels (CString m_workingDir)
     CString str;
     CString strOneLine;
     char buf[512];

     WIN32_FIND_DATA fd;
     m_workingDir.Format("%s\\*.asm", m_workingDir);
     HANDLE hFind = ::FindFirstFile ( _T(m_workingDir), &fd);

     if ( hFind != INVALID_HANDLE_VALUE ) {
          do {
               if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {    
                    CFile asmFile( (int) hFind );
                    CArchive asmArchive( &asmFile, CArchive::load, 512, buf );

                    // read data in, one line at a time
                    while(asmArchive.ReadString(strOneLine)) {
                                     // DO SOME STUFF
          } while (::FindNextFile (hFind, &fd));
          ::FindClose (hFind);

     return &m_ASMLabelNames;
Question by:bpwallac
LVL 12

Expert Comment

ID: 8127080
Question seems very big. Cant' read. I give up...

Accepted Solution

gelbert earned 150 total points
ID: 8131697
Do not use return handle from FindFirstFile() to open file in CFile(). Use file name (cFileName) from WIN32_FIND_DATA structure. Just make sure that file name which you passing into CFile() is fully qualified.

Author Comment

ID: 8148795
Thanks for the tip....that is definitely what I was doing wrong.  I the function is working well now.

I guess I just don't understand exactly what a HANDLE is in Win32.  When I looked at the constructor documentation for CFile,

CFile( int hFile );

I assumed that the argument is a handle to a file.  I guess I was wrong...

Anyways, it is working now...Thank you!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

578 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