how to write and save ini files like some apps do with their ini's
-
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 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
Not sure I understand, why is
WritePrivateProfileString()
insufficient for your app? The INI file looks like you describe, andWritePrivateProfileString()
can delete entries or whole sections. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true. -
Not sure I understand, why is
WritePrivateProfileString()
insufficient for your app? The INI file looks like you describe, andWritePrivateProfileString()
can delete entries or whole sections. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.http://adamc.hypermart.net/TESTINI.zip here is my app my friend told me to use loop in that testini() section. it do not work. look for this section. void CINIFileTestDlg::OnTestini() { // TODO: Add your control notification handler code here char sIniFile[MAX_PATH]; GetModuleFileName( NULL, sIniFile, MAX_PATH ); strcpy( strrchr( sIniFile, '\\' ) + 1, "TESTTT.ini" ); char buffer[1024]; ///for(int x=0; x < GetnCount(); ++x) for(int x=0; x < GetnCount(); ++x) { sprintf(buffer,"n%d",x); WritePrivateProfileString("TEST", buffer, "STRING", sIniFile); } m_ncount++;
-
http://adamc.hypermart.net/TESTINI.zip here is my app my friend told me to use loop in that testini() section. it do not work. look for this section. void CINIFileTestDlg::OnTestini() { // TODO: Add your control notification handler code here char sIniFile[MAX_PATH]; GetModuleFileName( NULL, sIniFile, MAX_PATH ); strcpy( strrchr( sIniFile, '\\' ) + 1, "TESTTT.ini" ); char buffer[1024]; ///for(int x=0; x < GetnCount(); ++x) for(int x=0; x < GetnCount(); ++x) { sprintf(buffer,"n%d",x); WritePrivateProfileString("TEST", buffer, "STRING", sIniFile); } m_ncount++;
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 a
-
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
Please again check out my new program.... there is problem with loop ;/ it should only do that thing ONE time each time you press 'add string' button. at this time im not using anyone's class. http://adamc.hypermart.net/TESTSTRINGS.zip
-
Please again check out my new program.... there is problem with loop ;/ it should only do that thing ONE time each time you press 'add string' button. at this time im not using anyone's class. http://adamc.hypermart.net/TESTSTRINGS.zip
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
-
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)