Working with INI Related functions
-
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."
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");
-
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");
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."
-
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."
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...
-
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."
-
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.
-
Hi!! The above code is also not working. Wat to do now? Thanx "If you change then change for the good."
-
Hi!! The above code is also not working. Wat to do now? Thanx "If you change then change for the good."
-
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."
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); }
-
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); }
-
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); }
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