Click here to Skip to main content
15,895,142 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to enumerate Input/Output devices ??? Pin
Monty228-Dec-03 21:01
Monty228-Dec-03 21:01 
GeneralRe: how to enumerate Input/Output devices ??? Pin
skpanda1-Jan-04 23:08
skpanda1-Jan-04 23:08 
QuestionHow to get all desktop icons? Pin
ioat25-Dec-03 19:16
ioat25-Dec-03 19:16 
GeneralRegarding drw_dxf.h Pin
cithu25-Dec-03 18:22
cithu25-Dec-03 18:22 
Questionhow to write and save ini files like some apps do with their ini's Pin
ELY M.25-Dec-03 18:04
ELY M.25-Dec-03 18:04 
AnswerRe: how to write and save ini files like some apps do with their ini's Pin
Michael Dunn25-Dec-03 19:12
sitebuilderMichael Dunn25-Dec-03 19:12 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.25-Dec-03 21:24
ELY M.25-Dec-03 21:24 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
shultas26-Dec-03 4:27
shultas26-Dec-03 4:27 
Ok. I am assuming that in your OnTestini() method, you want to READ from the .INI file, to test out the fact that it was created?

So, if that is the case. What you need to do is a call to GetPrivateProfileString(). That will read the .INI file for you and populate a variable.

It appears as though you are using a class that someone else created to do your Write/Get to the .INI file. If you are trying to do a basic .INI file, this is unnecessary because you can use the built in methods GetPrivateProfileString() and WritePrivateProfileString() yourself to do the task!

So, first off, lets take the code that you have now, and make a little modification to the OnTestini() method. Lets make this method actually read values from the .INI file. I am adapting this so it looks just like your current code. How this works is this. You run your app. Type in c:\test.ini in the filename.ini editbox (You must use a relative path or GetPrivateProfileString() may fail depending on where it gets run from). Type in something for the section/name and value fields. Then, press MAKE INI. It will create c:\test.ini for you. (That part in your code worked OK). Next, if you want to be sure, delete out the value that you typed in for the key value. Then, press TEST INI button. What happens is the following code figures out which key you want from your editboxes and then tries a GetPrivateProfileString() to get that data for you. (And it will succeed too!). It'll then give you a MessageBox with the key value. The code modification is below...

Also, what I would recommend is doing something like this. Start a new MFC dialog based app. Add a test ini and a make ini button along with your edit boxes. After you've created the editboxes, right click anywhere on the dialog you are creating, and select ClassWizard from the menu that pops up. Then, click on the Member Variables Tab. Scroll down to your IDC_EDIT1 and click on Add Variable. It'll ask for the member variable name, type something in there like m_fileName. Leave the default values in there for the rest (Category: Value and Variable Type: CString) Then, do that for each of your editboxes, do some variables like m_secName, m_keyName, m_keyValue. Once you do that, you can then access m_fileName,m_secName,m_keyName,m_keyValue from any member method of your class. So currently you have things like this:
CEdit* getsection = (CEdit*)GetDlgItem(IDC_EDIT7);
getsection->GetWindowText(putsection);
MessageBox(putsection); // Shows whatever was in IDC_EDIT7 edit box

If you do what I am suggesting, you can then do this in your code.
UpdateData(TRUE); // Do this ONCE before you try to access this variable.
MessageBox(m_keyName); // Shows whatever was in that edit box!

Then, once you have that in place, you can create your INI file like this
WritePrivateProfileString(m_secName,m_keyName,m_keyValue,m_fileName);

Then, when you want to read a value:
char buf[80];
GetPrivateProfileString(m_secName,m_keyName,"A DEFAULT VALUE",buf,80,m_fileName);
MessageBox(m_keyName + " in the ini file has a value of " + buf);

It's as simple as that! The "A DEFAULT VALUE" is a default value to use in case the read failed. (IE It can't find the section or key that you are looking for, so it returns a default value)

Drop a line if something does not make sense. Hopefully I explained it enough!

-Shultas




void CINIFileTestDlg::OnTestini()
{
// TODO: Add your control notification handler code here

char buffer[1024];

char sIniFile[MAX_PATH];

CEdit* getfilename = (CEdit*)GetDlgItem(IDC_EDIT1);
CString putfilename;
getfilename->GetWindowText(putfilename);

CEdit* getsection = (CEdit*)GetDlgItem(IDC_EDIT7);
CEdit* getname = (CEdit*)GetDlgItem(IDC_EDIT2);
CString putsection;
CString putname;
char myBuf[80];

getsection->GetWindowText(putsection);
getname->GetWindowText(putname);
//WritePrivateProfileString(putsection,putname,"key value here","test.ini");
GetPrivateProfileString(putsection,putname,"default value",myBuf,80,putfilename);
MessageBox(myBuf);
}
AnswerRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.26-Dec-03 12:49
ELY M.26-Dec-03 12:49 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
shultas26-Dec-03 14:01
shultas26-Dec-03 14:01 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.26-Dec-03 14:44
ELY M.26-Dec-03 14:44 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
shultas26-Dec-03 16:10
shultas26-Dec-03 16:10 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.26-Dec-03 18:42
ELY M.26-Dec-03 18:42 
AnswerRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.26-Dec-03 16:01
ELY M.26-Dec-03 16:01 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
shultas26-Dec-03 17:03
shultas26-Dec-03 17:03 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.27-Dec-03 5:22
ELY M.27-Dec-03 5:22 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
shultas28-Dec-03 10:08
shultas28-Dec-03 10:08 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.29-Dec-03 5:01
ELY M.29-Dec-03 5:01 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.29-Dec-03 5:20
ELY M.29-Dec-03 5:20 
GeneralRe: how to write and save ini files like some apps do with their ini's Pin
ELY M.29-Dec-03 6:34
ELY M.29-Dec-03 6:34 
GeneralPostMessage Problem Pin
percyvimal25-Dec-03 18:00
percyvimal25-Dec-03 18:00 
GeneralRe: PostMessage Problem Pin
Monty225-Dec-03 20:39
Monty225-Dec-03 20:39 
GeneralCSingleLock Pin
Anonymous25-Dec-03 17:10
Anonymous25-Dec-03 17:10 
GeneralRe: CSingleLock Pin
Michael Dunn25-Dec-03 19:15
sitebuilderMichael Dunn25-Dec-03 19:15 
GeneralRe: CSingleLock Pin
Anonymous25-Dec-03 20:30
Anonymous25-Dec-03 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.