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. Working with INI Related functions

Working with INI Related functions

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++xmltutorialquestion
16 Posts 5 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.
  • S swaapu

    Hi! iam working on an mfc sdi project in which , i hav to read ini file and to change it into its corresponding xml file. I am doing it with the help of GetPrivateProfileSectionNAmes and GetPrivateProfileSection functions. My sample ini file is: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] key3=500 key4=100 [SECTION3] key5=xyz My code is like dis: void CNewIni2XmlDlg::OnBConvert() { // TODO: Add your control notification handlcode here FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\users\\SG\\SWATI.INI","r"); fpw = fopen("C:\\users\\SG\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); GetPrivateProfileSectionNames(buffer,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n<%s>",buffer); fflush(fpw); GetPrivateProfileSection(buffer,buff,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n%s",buff); fclose(fpr); fclose(fpw); AfxMessageBox("ok"); } Now, the problem is , this method is returning only the SECTION1 and KEY1=500. ##How to get names of all the remaining sections?? ##What should , I do to get all the key value pairs for any section?? Thanx a lot for ur kind support. "If you change then change for the good."

    _ Offline
    _ Offline
    _anil_
    wrote on last edited by
    #2

    I suggest to check the buffer again after this statement. GetPrivateProfileSectionNames(buffer,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n<%s>",buffer); you might be getting all the names but problem is in the fprintf statemet. fprintf will end at the first occurance of a null charachetr. but GetPrivateProfileSectionNames fill the buffers with section names separated with null characters and the last one with two null characters.

    S 1 Reply Last reply
    0
    • S swaapu

      Hi! iam working on an mfc sdi project in which , i hav to read ini file and to change it into its corresponding xml file. I am doing it with the help of GetPrivateProfileSectionNAmes and GetPrivateProfileSection functions. My sample ini file is: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] key3=500 key4=100 [SECTION3] key5=xyz My code is like dis: void CNewIni2XmlDlg::OnBConvert() { // TODO: Add your control notification handlcode here FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\users\\SG\\SWATI.INI","r"); fpw = fopen("C:\\users\\SG\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); GetPrivateProfileSectionNames(buffer,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n<%s>",buffer); fflush(fpw); GetPrivateProfileSection(buffer,buff,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n%s",buff); fclose(fpr); fclose(fpw); AfxMessageBox("ok"); } Now, the problem is , this method is returning only the SECTION1 and KEY1=500. ##How to get names of all the remaining sections?? ##What should , I do to get all the key value pairs for any section?? Thanx a lot for ur kind support. "If you change then change for the good."

      J Offline
      J Offline
      Jagadeesh VN
      wrote on last edited by
      #3

      The output buffer from GetPrivateProfileSectionNames is filled with all the section names and are seperated by a null character. The last section name is followed by two null characters. Similarly output buffer in GetPrivateProfileSection is also filled with all the key name, value pairs seperated by null character. So modify your program accordingly... "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors." XMinds

      S 1 Reply Last reply
      0
      • _ _anil_

        I suggest to check the buffer again after this statement. GetPrivateProfileSectionNames(buffer,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n<%s>",buffer); you might be getting all the names but problem is in the fprintf statemet. fprintf will end at the first occurance of a null charachetr. but GetPrivateProfileSectionNames fill the buffers with section names separated with null characters and the last one with two null characters.

        S Offline
        S Offline
        swaapu
        wrote on last edited by
        #4

        Hello Anil! But, I need more help. Could u Plz tell me how should i write in the file, if with fprintf, it is not possible. I am new to VC++. Thanx a lot "If you change then change for the good."

        _ 1 Reply Last reply
        0
        • J Jagadeesh VN

          The output buffer from GetPrivateProfileSectionNames is filled with all the section names and are seperated by a null character. The last section name is followed by two null characters. Similarly output buffer in GetPrivateProfileSection is also filled with all the key name, value pairs seperated by null character. So modify your program accordingly... "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors." XMinds

          S Offline
          S Offline
          swaapu
          wrote on last edited by
          #5

          HI!! Could u plz tell me how to modify the program , to print all the section names. Thanx "If you change then change for the good."

          J 1 Reply Last reply
          0
          • S swaapu

            HI!! Could u plz tell me how to modify the program , to print all the section names. Thanx "If you change then change for the good."

            J Offline
            J Offline
            Jagadeesh VN
            wrote on last edited by
            #6

            Checkout this URL. http://www.codeguru.com/Cpp/data/data-misc/inifiles/article.php/c4577/ "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors." XMinds

            1 Reply Last reply
            0
            • S swaapu

              Hello Anil! But, I need more help. Could u Plz tell me how should i write in the file, if with fprintf, it is not possible. I am new to VC++. Thanx a lot "If you change then change for the good."

              _ Offline
              _ Offline
              _anil_
              wrote on last edited by
              #7

              don't know XML at all. tell me in which format do you want? I changed your program as I guess your requirment. FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\SWATI.INI","r"); fpw = fopen("C:\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); memset(buffer, 0x00, 50); GetPrivateProfileSectionNames(buffer,nSize,"C:\\SWATI.INI"); char* pBuffer = buffer; int nWrite = 0; while(strlen(pBuffer) > 0){ nWrite = fprintf(fpw,"\n<%s>",pBuffer); memset(buff, 0x00, 50); GetPrivateProfileSection(pBuffer,buff,nSize,"C:\\SWATI.INI"); char* pBuff = buff; int nWrt = 0; while(strlen(pBuff) > 0){ nWrt = fprintf(fpw,"\n%s",pBuff); pBuff = pBuff + nWrt; } pBuffer = pBuffer + nWrite - 2; fflush(fpw); } fclose(fpr); fclose(fpw); AfxMessageBox("ok");

              S 1 Reply Last reply
              0
              • _ _anil_

                don't know XML at all. tell me in which format do you want? I changed your program as I guess your requirment. FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\SWATI.INI","r"); fpw = fopen("C:\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); memset(buffer, 0x00, 50); GetPrivateProfileSectionNames(buffer,nSize,"C:\\SWATI.INI"); char* pBuffer = buffer; int nWrite = 0; while(strlen(pBuffer) > 0){ nWrite = fprintf(fpw,"\n<%s>",pBuffer); memset(buff, 0x00, 50); GetPrivateProfileSection(pBuffer,buff,nSize,"C:\\SWATI.INI"); char* pBuff = buff; int nWrt = 0; while(strlen(pBuff) > 0){ nWrt = fprintf(fpw,"\n%s",pBuff); pBuff = pBuff + nWrt; } pBuffer = pBuffer + nWrite - 2; fflush(fpw); } fclose(fpr); fclose(fpw); AfxMessageBox("ok");

                S Offline
                S Offline
                swaapu
                wrote on last edited by
                #8

                Hello Anil!! I m providing u here with a sample ini file and its converted form in XML. For Ini File: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] KEY3=500 KEY4=100 [SECTION3] KEY5=XYZ This is the XML format: 500 VALUE 500 100 xyz Thanx a lot for ur so kind support.Bye "If you change then change for the good."

                _ 1 Reply Last reply
                0
                • S swaapu

                  Hi! iam working on an mfc sdi project in which , i hav to read ini file and to change it into its corresponding xml file. I am doing it with the help of GetPrivateProfileSectionNAmes and GetPrivateProfileSection functions. My sample ini file is: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] key3=500 key4=100 [SECTION3] key5=xyz My code is like dis: void CNewIni2XmlDlg::OnBConvert() { // TODO: Add your control notification handlcode here FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\users\\SG\\SWATI.INI","r"); fpw = fopen("C:\\users\\SG\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); GetPrivateProfileSectionNames(buffer,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n<%s>",buffer); fflush(fpw); GetPrivateProfileSection(buffer,buff,nSize,"C:\\users\\SG\\SWATI.INI"); fprintf(fpw,"\n%s",buff); fclose(fpr); fclose(fpw); AfxMessageBox("ok"); } Now, the problem is , this method is returning only the SECTION1 and KEY1=500. ##How to get names of all the remaining sections?? ##What should , I do to get all the key value pairs for any section?? Thanx a lot for ur kind support. "If you change then change for the good."

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #9

                  Using INI files is one of the simpilist things in the world. If you read the documentation, you'll note that the names returned by GetPrivateProfileSectionNames() are each null terminated, that means when you pass it to GetPrivateProfileSection(), it only sees the first one in the list. You need to take into account the return value of GetPrivateProfileSectionNames(), which is the number of characters retreived, and parse it to get each section name. Example of what GetPrivateProfileSectionNames() should return: "SECTION1\0SECTION2\0SECTION3\0\0". The same applies to what is returned by GetPrivateProfileSection(). In your case the buffer should actualy contain something like: "KEY1=500\0KEY2=VALUE\0\0". This is anouther case where you need to pay attention to the return value, and manualy parse buffer. Some Notes: 1) There are other functions that read the specific values given the section and key names, they are easier to use. This assumes that your program already knows the section names and the key names. 2) After the first call the .INI is loaded into memory, so there is almost no overhead associated with reading the file on subsiquent calls. Because it is already loaded into memory space. 3) Your code is written in 'C' not 'C++', so you should provide a define statement ("#define") that specifies the maximum size of the buffer. That way you can change it by simply changing the value defined. In this case you do have a second option, if you dinamicaly allocate the buffer and the value return is rediculously high, then the value returned is actualy the negitive size of the required buffer. The only reason that it is rediculoulsy high, is that both functions return a DWORD, which is unsigned. If you take that value and convert it to a long and subtract it from 0, then you should have the required number of bytes needed to hold all the data, at which point you can reallocate the buffer and recall the function. What all the above amounts to, is that you need to read and understand the documentation, before you use the functions. It also means that I have never had a reason to use those two particular functions, because of note 1. I hope you understand what I said, because it is very important that you understand what I consider the fundamentals of programming. After a while you'll be seeing these things in your sleep. Good Luck, John R. Shaw INTP Every thing is relative...

                  1 Reply Last reply
                  0
                  • S swaapu

                    Hello Anil!! I m providing u here with a sample ini file and its converted form in XML. For Ini File: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] KEY3=500 KEY4=100 [SECTION3] KEY5=XYZ This is the XML format: 500 VALUE 500 100 xyz Thanx a lot for ur so kind support.Bye "If you change then change for the good."

                    _ Offline
                    _ Offline
                    _anil_
                    wrote on last edited by
                    #10

                    Try to understand the above code. There are lots of way to do. May be you are new to VC thats why getting problem. the format you posted is not right as <> are not displayed. Change the above code, if not able to do then ask again.

                    S 1 Reply Last reply
                    0
                    • _ _anil_

                      Try to understand the above code. There are lots of way to do. May be you are new to VC thats why getting problem. the format you posted is not right as <> are not displayed. Change the above code, if not able to do then ask again.

                      S Offline
                      S Offline
                      swaapu
                      wrote on last edited by
                      #11

                      Hi!! The above code is also not working. Wat to do now? Thanx "If you change then change for the good."

                      _ S 2 Replies Last reply
                      0
                      • S swaapu

                        Hi!! The above code is also not working. Wat to do now? Thanx "If you change then change for the good."

                        _ Offline
                        _ Offline
                        _anil_
                        wrote on last edited by
                        #12

                        mail me the output format if you post it then click the ignore HTML tags and then post. or modify your previous post with HTML tag ignored.

                        1 Reply Last reply
                        0
                        • S swaapu

                          Hi!! The above code is also not working. Wat to do now? Thanx "If you change then change for the good."

                          S Offline
                          S Offline
                          swaapu
                          wrote on last edited by
                          #13

                          Hi!! For INI file: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] KEY3=500 KEY4=100 [SECTION3] KEY5=xyz The corresponding XML file would be: 500 VALUE 500 100 xyz "If you change then change for the good."

                          _ 1 Reply Last reply
                          0
                          • S swaapu

                            Hi!! For INI file: [SECTION1] KEY1=500 KEY2=VALUE [SECTION2] KEY3=500 KEY4=100 [SECTION3] KEY5=xyz The corresponding XML file would be: 500 VALUE 500 100 xyz "If you change then change for the good."

                            _ Offline
                            _ Offline
                            _anil_
                            wrote on last edited by
                            #14

                            Try this one... { FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\SWATI.INI","r"); fpw = fopen("C:\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); memset(buffer, 0x00, 50); GetPrivateProfileSectionNames(buffer,nSize,"C:\\SWATI.INI"); char* pBuffer = buffer; int nWrite = 0; while(strlen(pBuffer) > 0){ // For all Section Name nWrite = fprintf(fpw,"\n<%s>",pBuffer); // Print the SectionName < SECTION > memset(buff, 0x00, 50); GetPrivateProfileSection(pBuffer,buff,nSize,"C:\\SWATI.INI"); char* pBuff = buff; int nWrt = 0; while(strlen(pBuff) > 0){ // For all KEY=VALUE CString strToken; strToken = pBuff; // The the KEY with Value "KEY=VALUE" // Find the = character inside the string INT nPosEql = strToken.Find('='); if(nPosEql == -1){ break; } CString strKey = strToken.Left(nPosEql); // Get the Key CString strVal = strToken.Mid(nPosEql + 1); // Get the value fprintf(fpw, "\t\n<%s>", strKey); // Print fprintf(fpw, "%s", strVal); // Print VALUE fprintf(fpw, "", strKey); // Print pBuff = pBuff + strlen(pBuff) + 1; // Move to next key } fprintf(fpw,"\n",pBuffer); // Print the pBuffer = pBuffer + strlen(pBuffer) + 1; //Move to next section } fclose(fpr); fclose(fpw); }

                            S A 2 Replies Last reply
                            0
                            • _ _anil_

                              Try this one... { FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\SWATI.INI","r"); fpw = fopen("C:\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); memset(buffer, 0x00, 50); GetPrivateProfileSectionNames(buffer,nSize,"C:\\SWATI.INI"); char* pBuffer = buffer; int nWrite = 0; while(strlen(pBuffer) > 0){ // For all Section Name nWrite = fprintf(fpw,"\n<%s>",pBuffer); // Print the SectionName < SECTION > memset(buff, 0x00, 50); GetPrivateProfileSection(pBuffer,buff,nSize,"C:\\SWATI.INI"); char* pBuff = buff; int nWrt = 0; while(strlen(pBuff) > 0){ // For all KEY=VALUE CString strToken; strToken = pBuff; // The the KEY with Value "KEY=VALUE" // Find the = character inside the string INT nPosEql = strToken.Find('='); if(nPosEql == -1){ break; } CString strKey = strToken.Left(nPosEql); // Get the Key CString strVal = strToken.Mid(nPosEql + 1); // Get the value fprintf(fpw, "\t\n<%s>", strKey); // Print fprintf(fpw, "%s", strVal); // Print VALUE fprintf(fpw, "", strKey); // Print pBuff = pBuff + strlen(pBuff) + 1; // Move to next key } fprintf(fpw,"\n",pBuffer); // Print the pBuffer = pBuffer + strlen(pBuffer) + 1; //Move to next section } fclose(fpr); fclose(fpw); }

                              S Offline
                              S Offline
                              swaapu
                              wrote on last edited by
                              #15

                              Hi!!!!!!!!!!!! Yes, it`s working. Thanx a lot for ur kind support. "If you change then change for the good."

                              1 Reply Last reply
                              0
                              • _ _anil_

                                Try this one... { FILE *fpr,*fpw; CHAR buffer[50]; DWORD nSize=50; LPTSTR lpszReturnBuffer; CHAR buff[50]; fpr = fopen("C:\\SWATI.INI","r"); fpw = fopen("C:\\Ss.txt","w"); lpszReturnBuffer = buff; fprintf(fpw,""); memset(buffer, 0x00, 50); GetPrivateProfileSectionNames(buffer,nSize,"C:\\SWATI.INI"); char* pBuffer = buffer; int nWrite = 0; while(strlen(pBuffer) > 0){ // For all Section Name nWrite = fprintf(fpw,"\n<%s>",pBuffer); // Print the SectionName < SECTION > memset(buff, 0x00, 50); GetPrivateProfileSection(pBuffer,buff,nSize,"C:\\SWATI.INI"); char* pBuff = buff; int nWrt = 0; while(strlen(pBuff) > 0){ // For all KEY=VALUE CString strToken; strToken = pBuff; // The the KEY with Value "KEY=VALUE" // Find the = character inside the string INT nPosEql = strToken.Find('='); if(nPosEql == -1){ break; } CString strKey = strToken.Left(nPosEql); // Get the Key CString strVal = strToken.Mid(nPosEql + 1); // Get the value fprintf(fpw, "\t\n<%s>", strKey); // Print fprintf(fpw, "%s", strVal); // Print VALUE fprintf(fpw, "", strKey); // Print pBuff = pBuff + strlen(pBuff) + 1; // Move to next key } fprintf(fpw,"\n",pBuffer); // Print the pBuffer = pBuffer + strlen(pBuffer) + 1; //Move to next section } fclose(fpr); fclose(fpw); }

                                A Offline
                                A Offline
                                alleyes 0
                                wrote on last edited by
                                #16

                                I have to create a console text menu that is written from values from an INI file. The menu entry would be something like: 1) Pencil 2) Pen 3) Typwriter Select choice --> The INI file has [Writing Tools] 1 = Pencil 2 = Pen 3 = Typewriter Can you modify the example to create a buffer (or array of struct) that uses this? I don't need values written to an output file. Thanks

                                Jer 29:11

                                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