INI file

Posted on 2000-04-25
Medium Priority
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.


Question by:tlnguyen
  • 5
  • 4
LVL 10

Expert Comment

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).

LVL 10

Expert Comment

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

#include <AfxAdv.h>

CString CMyWinApp::MostRecentFile() {
    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();


Author Comment

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.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LVL 10

Expert Comment

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);

Author Comment

ID: 2753920
Adjusted points from 50 to 78

Author Comment

ID: 2753921

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);

LVL 10

Accepted Solution

RONSLOW earned 234 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).

Author Comment

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.
LVL 10

Expert Comment

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?


Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
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.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

607 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