Solved

INI file

Posted on 2000-04-25
9
708 Views
Last Modified: 2013-11-20
I'm using Visual C++ & MFC library.
How do I save setting the current open file and its path into an .INI file?.
When the next time the application run, it will automatically open the same file by reading the filename from the same .INI file.

Please help.

Thanks,

TN
0
Comment
Question by:tlnguyen
[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
  • 5
  • 4
9 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2750074
GetProfileString and WriteProfileString if you want to do it yourself.

However, MFC already does this for you in its MRU file list (stored in the registry).  You can ask MFC to open the most file(from last time it was run).

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2750080
Here is a function to get at the most recently used file...

#include <AfxAdv.h>

CString CMyWinApp::MostRecentFile() {
    ASSERT_VALID(this);
    ASSERT(m_pRecentFileList != NULL);
    if (m_pRecentFileList->GetSize() < 1) {
        return ::afxEmptyString;
    } else {
        return (*m_pRecentFileList)[0];
    }
}

what I do is have an option at startup to reropen the last file.  I then change the CCommandLineInfo variable as follows:

    cmdInfo->m_nShellCommand = CCommandLineInfo::FileOpen;
    cmdInfo->m_strFileName = MostRecentFile();

0
 

Author Comment

by:tlnguyen
ID: 2752266
This is excellent answer.  However, I am interested in save setting in the .INI file.  When I used WriteProfileString method and run the application from the floppy disk, it doesn't create and write the file to floppy/HD.  It works when run from the hard drive.

Please help.

TN
0
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!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2753514
INI files are discouraged in Win32 .. the registry is the preferred and default way place for storing such things.  IN fact, Win2000 will try very hard to keep ini files in the registry .. although you can still use files on disk/floppy.

Use WritePrivateProfileString and GetPrivateProfileString to use disk-based ini files (as opposed to the registry).

MFC's MRU list is stil stored in the registry, however, so you'll need to do the same sort of thing for you private (.ini file) setting.  Look at how the CRecentFileList does its work ... note that the functions to read and write info to the registry are virtual, so you can override them to force a write to your particular ini file.

To do this, you'll need to derive your own class from CRecentFileList and provide overrides for the ReadList and WriteList functions.

You'll also need to change the call to
LoadStdProfileSettings in you app so that it calls your own version.  Simply copy the MFC source for this function and change the CRecentFileList to your own class in the line...

    // create file MRU since nMaxMRU not zero
    m_pRecentFileList = new CRecentFileList(0, _afxFileSection, _afxFileEntry, nMaxMRU);
    m_pRecentFileList->ReadList();
0
 

Author Comment

by:tlnguyen
ID: 2753920
Adjusted points from 50 to 78
0
 

Author Comment

by:tlnguyen
ID: 2753921
RonSlow,

Thanks for your help.  I tried your code: CString CMyWinApp::MostRecentFile().  However, VC++ compiler issued too many error messages.  All I need is how to get the MostRecentFile from the MRU list so that I can write it out to the .INI file when the user chooses SAVE SETTING from the menu.

void CFileView::OnFileSavesetting()
{

CString strSection    = "Section";
CString strStringItem = "My Program";
CString strStringValue = MostRecentFile;

CWinApp* pApp = AfxGetApp();
 
pApp->WriteProfileString(strSection, strStringItem, strStringValue);

}
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 78 total points
ID: 2754006
You use the function I gave you .. the error are probably because you didn't have the #include that I put there for <afxadv.h> ... you need that for the CRecentFileList class.

Then you'll be able to use your code (but put () after MostRectentFile) and all should be well.

EXCEPT that WriteProfileString needs to be WritePrivateProfileString (as per earlier message).

PS: It is bad EE ettiquette to keep rejecting when you just want clarification or further discussions with the same expert).
0
 

Author Comment

by:tlnguyen
ID: 2754075
Well!, your code didn't compile even I added #include <AfxAdv.h>

Error Messages:

-error C2673: 'MostRecentFile' : global functions do not have 'this' pointers
-error C2065: 'm_pRecentFileList' : undeclared identifier
-error C2227: left of '->GetSize' must point to class/struct/union
-error C2100: illegal indirection
-error C2109: subscript requires array or pointer type

Thanks for trying to help me.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754110
You could have let me get rid of those errors before grading with a "B".  Giving a "B" grade without giving me the opportunity to fix up your problems is also not good EE ettiquette.

The errors are simply because you didn't do what I suggested, and so got error messages.  If you have coded as i suggetsed, all would be fine.  It is quite obvious what the problem is.

I can help you fix these errors.

Do you want to ask another question (now you've graded this one) so I can fix it up for you?

0

Featured Post

Independent Software Vendors: 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

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

696 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