Solved

INI file

Posted on 2000-04-25
9
701 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 76
wait notify demo infinite loop 3 81
matchUp  challenge 9 72
Use of condition with 'serial' in ansible 2 37
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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now