?
Solved

FindFirstFile, CFile, and CArchive

Posted on 2003-03-13
3
Medium Priority
?
400 Views
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
                    }
                    asmArchive.Close();
                    asmFile.Close();
               }
          } while (::FindNextFile (hFind, &fd));
          ::FindClose (hFind);
     }

     return &m_ASMLabelNames;
}
0
Comment
Question by:bpwallac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8127080
Question seems very big. Cant' read. I give up...
0
 
LVL 8

Accepted Solution

by:
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.
0
 

Author Comment

by:bpwallac
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!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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