Solved

INI file

Posted on 2000-04-25
9
702 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now