how to write and save ini files like some apps do with their ini's
-
Okie. I had a look and I see the problem. First off, what exactly is it that you are trying to do with your program? Then I can be direct and show you the code necessary. For now, I'll show you what is wrong with the loop. Here's your code: int m_ncount = 1; CString KeyName; for(int x=0; x < m_ncount; ++x) { KeyName.Format("n%ld", x); ///WritePrivateProfileString("TEST", buffer, putstring, sIniFile); WritePrivateProfileString("TEST", KeyName, putstring, sIniFile); m_ncount++; } If you look carefully, you will see the reason your program locks up is because you have created an infinate loop! The condition "x < m_ncount" will never be true, because you are incrementing the m_ncount variable at the end of the loop! So, the first time that the loop runs, m_ncount=1 and x=0. The second pass, m_ncount=2; x=1 ... Third pass, m_ncount=3;x=2. So each pass, the variable x gets incremented and so does the variable m_ncount! So therefore the loop will run forever! Post back and let me know what you are trying to do and I'll throw something together for you real fast to take example of! Shultas
-
Im working on app that will keep lists of things in a ini file from edit boxes on a dialogs. I already know about CEdit and WritePrivateProfileStrings things. but I need things to be listed like this. also I will add ability to delete. mIRC does this same way with the servers.ini and alias.ini [thing] n0=here n1=food n2=metal n3=hotdogs n4=metals n5=pencils n6=pens n7=paint for your info. Im using MFC to make my program in Visual C++ 6.0. here is my example of program http://adamc.hypermart.net/TESTSTRINGS.zip
Im trying to build same application as Zion mirc editor http://adamc.hypermart.net/Zion.zip look carefully and see how it does with writing and saving ini files. I know its in spanish ? but you can understand how the program works. I think its MFC app. I cant find source codes of similar programs.
-
Im trying to make it increament the strings like this each time you press the button. n0=string1 n1=string2 n2-string3 n3=string4 I know what problem im having ;/ Im not sure how to do it right way.
Okay. Gotcha. What I would do is this. Open up your classview and add a new member to the declaration of your CTESTINI2Dlg. class CTESTINI2Dlg : public CDialog { ....... ....... protected: HICON m_hIcon; int topKeyValue; // <------ Add this in there Then, expand the view for the CTESTINI2Dlg. Double click on the OnInitDialog() method. Look for the following lines (at the end of this method) // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon topKeyValue = 0; // <----------- Add that in there Then, double click on your OnAddStrings() method. This should go in there INSTEAD of the for{} loop. Get rid of that loop. void CTESTINI2Dlg::OnAddstrings() { char sIniFile[MAX_PATH]; GetModuleFileName( NULL, sIniFile, MAX_PATH ); strcpy( strrchr( sIniFile, '\\' ) + 1, "strings.ini" ); CEdit* getstring = (CEdit*)GetDlgItem(IDC_EDIT4); CString putstring; getstring->GetWindowText(putstring); CString KeyName; topKeyValue++; KeyName.Format("n%ld", topKeyValue); WritePrivateProfileString("TEST", KeyName, putstring, sIniFile); MessageBox("Added..."); } Okay. What this does is add an integer variable to your CTESTINI2Dlg class named topKeyValue. Then, in your OnInitDialog() code, topKeyValue = 0; that is initializing it to 0 when the program starts up. Then, in your OnAddstrings() method, topKeyValue++ increments the member variable by one, formats it in the KeyName.Format string, and writes that number accordingly. Each time you press the add strings button, topKeyValue gets incremented by 1, thus allowing you to write a new line into the file! So, what are you trying to do, screw with someones MIRC INI files? ;-)
-
Im trying to build same application as Zion mirc editor http://adamc.hypermart.net/Zion.zip look carefully and see how it does with writing and saving ini files. I know its in spanish ? but you can understand how the program works. I think its MFC app. I cant find source codes of similar programs.
lol. One heck of a guess on my part, huh? :-) I did get a chance to look at the app but really don't know what it is supposed to do and whatnot? Is it that you are maybe looking to add entries to server names, or channel names or something like that? What you have to do is look at the INI (as you've already done, obviously) and figure out exactly which fields you want to be able to modify. Take the mirc.ini for example. Lets say you wanted to modify [chanfolder] and add/edit/delete entries in there. I'll give you a starting point. First you would need to perform GetPrivateProfileString() on the [chanfolder] section until you reached the end. I would do that with some code like this: void CYOUR_DIALOG_NAME_HEREDlg::OnButton1() { char curKey[80], keyName[80]; m_topChanEntries = 0; while (1) { sprintf(keyName,"n%d",m_topChanEntries); GetPrivateProfileString("chanfolder",keyName,"DEFAULT",curKey,80,"c:\\program files\\mirc\\mirc.ini"); if (strstr(curKey,"DEFAULT")) { // MessageBox("Reached the top number..."); m_topChanEntries--; break; } // populate whatever controls you want to here with the current values m_topChanEntries++; } // Out of the loop -- variable "m_topChanEntries" now contains the # of entries in the //[chanfolders] section. } Of course, during the run, you will want to populate your editbox, listbox, or whatever boxes you are choosing to perform the editing on. If there are 30 entries in there, the while loop will go 30 times, then the strstr() portion will figure out that there are no more entries (because "DEFAULT" will be in there, because GetPrivateProfileString() could not get #31). That should be a good starting point for you to work with. What you would have to do for this to work is create an int member variable in your class delcaration under the protected section. Also, in your OnInitDialog() method, be sure to initialize this variable to 0. This way, after this button press code is finished executing, that variable will hold the number of entries currently in that [chanfolders] section. This way, you could add another button to, lets say, add another channel to the list, and then in that code you would just do something such as: sprintf(keyName,"n%d",++m_topChanEntries); // this increments the topChanEntries number first WritePrivateProfileString(...); Hope that helps! Shultas
-
Okay. Gotcha. What I would do is this. Open up your classview and add a new member to the declaration of your CTESTINI2Dlg. class CTESTINI2Dlg : public CDialog { ....... ....... protected: HICON m_hIcon; int topKeyValue; // <------ Add this in there Then, expand the view for the CTESTINI2Dlg. Double click on the OnInitDialog() method. Look for the following lines (at the end of this method) // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon topKeyValue = 0; // <----------- Add that in there Then, double click on your OnAddStrings() method. This should go in there INSTEAD of the for{} loop. Get rid of that loop. void CTESTINI2Dlg::OnAddstrings() { char sIniFile[MAX_PATH]; GetModuleFileName( NULL, sIniFile, MAX_PATH ); strcpy( strrchr( sIniFile, '\\' ) + 1, "strings.ini" ); CEdit* getstring = (CEdit*)GetDlgItem(IDC_EDIT4); CString putstring; getstring->GetWindowText(putstring); CString KeyName; topKeyValue++; KeyName.Format("n%ld", topKeyValue); WritePrivateProfileString("TEST", KeyName, putstring, sIniFile); MessageBox("Added..."); } Okay. What this does is add an integer variable to your CTESTINI2Dlg class named topKeyValue. Then, in your OnInitDialog() code, topKeyValue = 0; that is initializing it to 0 when the program starts up. Then, in your OnAddstrings() method, topKeyValue++ increments the member variable by one, formats it in the KeyName.Format string, and writes that number accordingly. Each time you press the add strings button, topKeyValue gets incremented by 1, thus allowing you to write a new line into the file! So, what are you trying to do, screw with someones MIRC INI files? ;-)
-
lol. One heck of a guess on my part, huh? :-) I did get a chance to look at the app but really don't know what it is supposed to do and whatnot? Is it that you are maybe looking to add entries to server names, or channel names or something like that? What you have to do is look at the INI (as you've already done, obviously) and figure out exactly which fields you want to be able to modify. Take the mirc.ini for example. Lets say you wanted to modify [chanfolder] and add/edit/delete entries in there. I'll give you a starting point. First you would need to perform GetPrivateProfileString() on the [chanfolder] section until you reached the end. I would do that with some code like this: void CYOUR_DIALOG_NAME_HEREDlg::OnButton1() { char curKey[80], keyName[80]; m_topChanEntries = 0; while (1) { sprintf(keyName,"n%d",m_topChanEntries); GetPrivateProfileString("chanfolder",keyName,"DEFAULT",curKey,80,"c:\\program files\\mirc\\mirc.ini"); if (strstr(curKey,"DEFAULT")) { // MessageBox("Reached the top number..."); m_topChanEntries--; break; } // populate whatever controls you want to here with the current values m_topChanEntries++; } // Out of the loop -- variable "m_topChanEntries" now contains the # of entries in the //[chanfolders] section. } Of course, during the run, you will want to populate your editbox, listbox, or whatever boxes you are choosing to perform the editing on. If there are 30 entries in there, the while loop will go 30 times, then the strstr() portion will figure out that there are no more entries (because "DEFAULT" will be in there, because GetPrivateProfileString() could not get #31). That should be a good starting point for you to work with. What you would have to do for this to work is create an int member variable in your class delcaration under the protected section. Also, in your OnInitDialog() method, be sure to initialize this variable to 0. This way, after this button press code is finished executing, that variable will hold the number of entries currently in that [chanfolders] section. This way, you could add another button to, lets say, add another channel to the list, and then in that code you would just do something such as: sprintf(keyName,"n%d",++m_topChanEntries); // this increments the topChanEntries number first WritePrivateProfileString(...); Hope that helps! Shultas
now i figured out how to do addstrings and stuff now I have one problem is how to delete strings correctly onuce you selected the string in combobox ? here is my updated app. http://adamc.hypermart.net/TESTSTRINGS2.zip
-
now i figured out how to do addstrings and stuff now I have one problem is how to delete strings correctly onuce you selected the string in combobox ? here is my updated app. http://adamc.hypermart.net/TESTSTRINGS2.zip
int npick; npick = m_list.GetCurSel(); That's what you have so far. To get the actual TEXT of the string that is in the listbox, you would do char keyName[80]; m_ListBox.GetText(npick,keyName); keyName will then have whatever value you selected in the listbox. You can then use the WriteProfileString() with a NULL in the value field to delete that keyName Shultas
-
int npick; npick = m_list.GetCurSel(); That's what you have so far. To get the actual TEXT of the string that is in the listbox, you would do char keyName[80]; m_ListBox.GetText(npick,keyName); keyName will then have whatever value you selected in the listbox. You can then use the WriteProfileString() with a NULL in the value field to delete that keyName Shultas
-
int npick; npick = m_list.GetCurSel(); That's what you have so far. To get the actual TEXT of the string that is in the listbox, you would do char keyName[80]; m_ListBox.GetText(npick,keyName); keyName will then have whatever value you selected in the listbox. You can then use the WriteProfileString() with a NULL in the value field to delete that keyName Shultas
Compiling... TESTINI2Dlg.cpp E:\-= IRC CLIENTS =-\TESTSTRINGS2\TESTINI2Dlg.cpp(410) : error C2039: 'GetText' : is not a member of 'CComboBox' d:\microsoft visual studio\vc98\mfc\include\afxwin.h(2893) : see declaration of 'CComboBox' Error executing cl.exe. TESTINI2.exe - 1 error(s), 0 warning(s) hmmmmmmmmmm
-
Compiling... TESTINI2Dlg.cpp E:\-= IRC CLIENTS =-\TESTSTRINGS2\TESTINI2Dlg.cpp(410) : error C2039: 'GetText' : is not a member of 'CComboBox' d:\microsoft visual studio\vc98\mfc\include\afxwin.h(2893) : see declaration of 'CComboBox' Error executing cl.exe. TESTINI2.exe - 1 error(s), 0 warning(s) hmmmmmmmmmm
void CTESTINI2Dlg::OnDeletestring() { // TODO: Add your control notification handler code here char sIniFile[MAX_PATH]; GetModuleFileName( NULL, sIniFile, MAX_PATH ); strcpy( strrchr( sIniFile, '\\' ) + 1, "strings.ini" ); /* char curKey[80], keyName[80]; m_entries = 0; while (1) { sprintf(keyName,"n%d",m_entries); GetPrivateProfileString("TEST",keyName,"DEFAULT",curKey,80,sIniFile); if (strstr(curKey,"DEFAULT")) { ///MessageBox("Reached the top number..."); /* CString text; text.Format("%d", m_entries); AfxMessageBox(text); */ /* m_entries--; break; } // populate whatever controls you want to here with the current values m_entries++; } */ int npick; npick = m_list.GetCurSel(); char keyName[80]; m_list.GetWindowText(npick, keyName); if(npick != CB_ERR) { WritePrivateProfileString("TEST", keyName, NULL, sIniFile); } } Result: Compiling... TESTINI2Dlg.cpp E:\-= IRC CLIENTS =-\TESTSTRINGS2\TESTINI2Dlg.cpp(416) : error C2664: 'int __thiscall CWnd::GetWindowTextA(char *,int) const' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. TESTINI2.exe - 1 error(s), 0 warning(s)