Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ERROR in _stricmp ..... prevent duplicate section names [modified]

ERROR in _stricmp ..... prevent duplicate section names [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anna mathew
    wrote on last edited by
    #1

    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

    C CPalliniC 2 Replies Last reply
    0
    • A anna mathew

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      1 Reply Last reply
      0
      • A anna mathew

        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

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3
        1. Use the code block button to surround your code snippet with <pre> tags, this way your code will be more readable.
        2. There's a logic flaw in your while loop, you should:
          1. initialize cmpflag (i.e. cmpflag=0;) before the while loop begins.
          2. set cmpflag=1; whenever you find a matching entry (and optionally use the break; statement to skip the loop, since there's no need for further comparison).

        :)

        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]

        In testa che avete, signor di Ceprano?

        A 1 Reply Last reply
        0
        • CPalliniC CPallini
          1. Use the code block button to surround your code snippet with <pre> tags, this way your code will be more readable.
          2. There's a logic flaw in your while loop, you should:
            1. initialize cmpflag (i.e. cmpflag=0;) before the while loop begins.
            2. set cmpflag=1; whenever you find a matching entry (and optionally use the break; statement to skip the loop, since there's no need for further comparison).

          :)

          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]

          A Offline
          A Offline
          anna mathew
          wrote on last edited by
          #4

          thanks.... i had initialized the flag... but i dint give break..... .....that was the ERROR

          CPalliniC 1 Reply Last reply
          0
          • A anna mathew

            thanks.... i had initialized the flag... but i dint give break..... .....that was the ERROR

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Well, the break statement hides the logic flaw, giving actually the correct result. Anyway, if you move the inizialization cmpflag=0; outside the while loop and don't use the break 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 remain 1. :)

            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]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups