?
Solved

INI file

Posted on 2000-04-25
9
Medium Priority
?
721 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

850 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