Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've created my first MFC SDI dialog based app with serialization using CArchive. I can open/read/write/close using the menu controls to select the file that I use, everything seems to work great. What I would like to do is open this file at program execution and read/write and close on program termination automatically. I think the CFile class is what I need, but I kindof stuck as where and how to use this. I'm using the SetModifiedFlag function, when my arrays are changed, are there going to be any issues with this function using CFile class?

OK, I have implemented the CFile open read/write and it does open the file for reading/writing, but it does not populate my dialog like it should. If I open the same file again from the menu inside my program, it populates like it should. What's weird is that I have created a message box that tells me the file has been opened for read/write when I invoke CFile and it works when I start my app and as I stated earlier it does not populate my dialog, sooo I tried to open the file again from my menu created by MFC and this opens the file again and populates my dialog and also shows me the messge box again, telling me that the file opened successfully. WHY!?!

What am I doing wrong?

Very confused.
Posted
Updated 9-Jun-11 14:04pm
v2
Comments
Resmi Anna 10-Jun-11 2:40am    
I am not getting a clear picture of your actual requirement from the post.
But it seems like you need to write something to your file while closing.And while opening you need to get it back and see in view.
If this is what you need
no need to write any CFile class or operate on it.
Your document class Serialize()methode does everything for you..
you add your code in that methode to handle open/close logic
DrBones69 10-Jun-11 21:23pm    
But, I would like to use the same data file every time I run the app. I have to manually open the data file at program startup. I would like my app to select this file at startup. What class and/or function should I use to accomplish this? Thanks

1 solution

Should be fine. I haven't had any problems with it.
 
Share this answer
 
Comments
DrBones69 9-Jun-11 20:43pm    
I know that the SetModifiedFlag function will still work, but can I make the app save the data automatically? And where do I use the CFile class within my MFC app? In other words, when & where should I open the file when the app loads so my app will automatically populate?
TRK3 10-Jun-11 12:35pm    
Really depends on what you are serializing, when you need it, and what needs to be initialized before you de-serialize it. You can always do it in MyApp::InitInstance() and MyApp::ExitInstance(). The problem with it populating your dialog box or not isn't related to the CFile class, it is related to when you are serializing / de-serializing relative to when/how you are populating the dialog box.
DrBones69 10-Jun-11 21:17pm    
It works the way I want it to if I open the data file manually. I'm just trying to find out what Windows is doing when I select the open command from my ribbon menu manually, whatever Windows is doing, thats what I want my app to do at first startup.
TRK3 13-Jun-11 19:07pm    
See: http://msdn.microsoft.com/en-us/library/zwk0509s(v=VS.80).aspx

And: http://msdn.microsoft.com/en-us/library/11861byt(v=VS.90).aspx

Which says in part:

"CWinApp::OnFileOpen has a very simple implementation of calling CWinApp::DoPromptFileName followed by CWinApp::OpenDocumentFile with the file or path name of the file to open. The CWinApp implementation routine DoPromptFileName brings up the standard FileOpen dialog and fills it with the file extensions obtained from the current document templates."

So, if I understand you correctly, the answer is, Windows is calling, CWinApp::OpenDocumentFile("yourfilename.ext");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900