ERROR in _stricmp ..... prevent duplicate section names [modified]
-
Please help me..... my sectionname is same as the name entered.... my code: void CFileINIDlg::OnBnClickedInsert() { const CString szINIFILE = ("C:\\FileINI.ini"); //Accept name , address and phone no from edit box m_Name.GetWindowText(Name); m_Ad.GetWindowText(Address); m_Phone.GetWindowText(Phoneno); CT2A sectn = Name; int cmp; int cmpflag=0; char message[4096]; strcpy (message, "Entries :\n\n"); //Initialize LPTSTR lpszReturnBuffer; lpszReturnBuffer = new TCHAR[MAX_PATH]; char* pNextSection = NULL; GetPrivateProfileSectionNames(lpszReturnBuffer,MAX_PATH,szINIFILE); pNextSection = lpszReturnBuffer; strcat(message, pNextSection); while (*pNextSection != NULL) { //to avoid overwriting entries with same section name cmp = _stricmp(sectn, pNextSection); if(cmp==0) cmpflag= 1; else cmpflag=0; pNextSection = pNextSection + strle(pNextSection) + 1; if(*pNextSection != NULL) { strcat(message, "\n"); strcat(message, pNextSection); } } sprintf(message,"%s\n CMP %d\n FLAG %d \n", message, cmp, cmpflag); MessageBox(message); if(cmpflag==1) MessageBox("ERROR - DUPLICATE"); else if(cmpflag==0) { WritePrivateProfileString(Name, "Name", Name, szINIFILE); WritePrivateProfileString(Name, "Address", Address, szINIFILE); WritePrivateProfileString(Name, "Phone", Phoneno, szINIFILE); } } Im writing the values entered to the INI file..... I want to prevent overwritng an entry ...that has the same section name....... if sectionname is same....i reject the entries insertion to INI file... my code compiles..... but strcmp() is not working..... it always returns 1..... how do i correct it?
modified on Wednesday, November 5, 2008 3:35 AM
-
Please help me..... my sectionname is same as the name entered.... my code: void CFileINIDlg::OnBnClickedInsert() { const CString szINIFILE = ("C:\\FileINI.ini"); //Accept name , address and phone no from edit box m_Name.GetWindowText(Name); m_Ad.GetWindowText(Address); m_Phone.GetWindowText(Phoneno); CT2A sectn = Name; int cmp; int cmpflag=0; char message[4096]; strcpy (message, "Entries :\n\n"); //Initialize LPTSTR lpszReturnBuffer; lpszReturnBuffer = new TCHAR[MAX_PATH]; char* pNextSection = NULL; GetPrivateProfileSectionNames(lpszReturnBuffer,MAX_PATH,szINIFILE); pNextSection = lpszReturnBuffer; strcat(message, pNextSection); while (*pNextSection != NULL) { //to avoid overwriting entries with same section name cmp = _stricmp(sectn, pNextSection); if(cmp==0) cmpflag= 1; else cmpflag=0; pNextSection = pNextSection + strle(pNextSection) + 1; if(*pNextSection != NULL) { strcat(message, "\n"); strcat(message, pNextSection); } } sprintf(message,"%s\n CMP %d\n FLAG %d \n", message, cmp, cmpflag); MessageBox(message); if(cmpflag==1) MessageBox("ERROR - DUPLICATE"); else if(cmpflag==0) { WritePrivateProfileString(Name, "Name", Name, szINIFILE); WritePrivateProfileString(Name, "Address", Address, szINIFILE); WritePrivateProfileString(Name, "Phone", Phoneno, szINIFILE); } } Im writing the values entered to the INI file..... I want to prevent overwritng an entry ...that has the same section name....... if sectionname is same....i reject the entries insertion to INI file... my code compiles..... but strcmp() is not working..... it always returns 1..... how do i correct it?
modified on Wednesday, November 5, 2008 3:35 AM
Use your debugger and check the contents of the strings. BTW, if you need to post code, please use the code tags, it makes your code much more readable (check also the posting guidelines at the top of this forum).
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Please help me..... my sectionname is same as the name entered.... my code: void CFileINIDlg::OnBnClickedInsert() { const CString szINIFILE = ("C:\\FileINI.ini"); //Accept name , address and phone no from edit box m_Name.GetWindowText(Name); m_Ad.GetWindowText(Address); m_Phone.GetWindowText(Phoneno); CT2A sectn = Name; int cmp; int cmpflag=0; char message[4096]; strcpy (message, "Entries :\n\n"); //Initialize LPTSTR lpszReturnBuffer; lpszReturnBuffer = new TCHAR[MAX_PATH]; char* pNextSection = NULL; GetPrivateProfileSectionNames(lpszReturnBuffer,MAX_PATH,szINIFILE); pNextSection = lpszReturnBuffer; strcat(message, pNextSection); while (*pNextSection != NULL) { //to avoid overwriting entries with same section name cmp = _stricmp(sectn, pNextSection); if(cmp==0) cmpflag= 1; else cmpflag=0; pNextSection = pNextSection + strle(pNextSection) + 1; if(*pNextSection != NULL) { strcat(message, "\n"); strcat(message, pNextSection); } } sprintf(message,"%s\n CMP %d\n FLAG %d \n", message, cmp, cmpflag); MessageBox(message); if(cmpflag==1) MessageBox("ERROR - DUPLICATE"); else if(cmpflag==0) { WritePrivateProfileString(Name, "Name", Name, szINIFILE); WritePrivateProfileString(Name, "Address", Address, szINIFILE); WritePrivateProfileString(Name, "Phone", Phoneno, szINIFILE); } } Im writing the values entered to the INI file..... I want to prevent overwritng an entry ...that has the same section name....... if sectionname is same....i reject the entries insertion to INI file... my code compiles..... but strcmp() is not working..... it always returns 1..... how do i correct it?
modified on Wednesday, November 5, 2008 3:35 AM
- Use the
code block
button to surround your code snippet with<pre>
tags, this way your code will be more readable. - There's a logic flaw in your while loop, you should:
- initialize
cmpflag
(i.e.cmpflag=0;
) before the while loop begins. - set
cmpflag=1;
whenever you find a matching entry (and optionally use thebreak;
statement to skip the loop, since there's no need for further comparison).
- initialize
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] - Use the
-
- Use the
code block
button to surround your code snippet with<pre>
tags, this way your code will be more readable. - There's a logic flaw in your while loop, you should:
- initialize
cmpflag
(i.e.cmpflag=0;
) before the while loop begins. - set
cmpflag=1;
whenever you find a matching entry (and optionally use thebreak;
statement to skip the loop, since there's no need for further comparison).
- initialize
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]thanks.... i had initialized the flag... but i dint give break..... .....that was the ERROR
- Use the
-
thanks.... i had initialized the flag... but i dint give break..... .....that was the ERROR
Well, the
break
statement hides the logic flaw, giving actually the correct result. Anyway, if you move the inizializationcmpflag=0;
outside thewhile
loop and don't use thebreak
statement, your code will still work fine, but is somewhat inefficient. YOu should note that you're searching for a matching entry and if you find it, no matter how many different entries there are,cmpflag
should remain1
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]