VC operation on INI file

Source: Internet
Author: User

1. See if the INI file exists and does not exist to create the INI file

    //See if you have a First.ini fileCFileFind FD; if(FD. FindFile (". First.int") {MessageBox ("Find File"); }    Else{MessageBox ("file not found, file created");           CFile file; File. Open (". \first.int", cfile::modecreate|cfile::modewrite); File.             Close (); }

2. Write an int CString to the INI file

// INI, create a Test section and create a key ID in the section with a value of Xym    :: WritePrivateProfileString ("Test", " ID ","xym","./first.int");
:: WritePrivateProfileString ("Test", "Age", "a", "./first.int");

3. Reading integers and strings

Read information from its own INI file
(1) reads the string
DWORD getprivateprofilestring (
      lpctstr Lpappname,           
      lpctstr lpkeyname,   & nbsp    &NBSP
      lpctstr Lpdefault,            
      lptstr lpreturnedstring,      
       dword NSize,            
       LPCTSTR lpfilename            //Read the file name of the information. If the INI file and the program in the same directory, you can also use the phase      
             //to the path, otherwise the absolute path needs to be given.
)

UINT Getprivateprofileint (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
INT Ndefault,//If the specified key name does not exist, the value is the default value for the Read
LPCTSTR lpFileName
)

    //ReadCString Strid; intAge=0; :: GetPrivateProfileString ("Test","ID","Error", Strid.getbuffer ( -), -,"./first.int"); Age=:: Getprivateprofileint ("Test"," Age",0,"./first.int");

Original: http://blog.sina.com.cn/s/blog_618b45a00100gsq0.html

How to read and write INI file with function in VC

INI file (that is, initialization file), which typically holds the initialization information for a program. INI file consists of several sections (section), each of which consists of a number of keys (key), each of which can be assigned a corresponding value. A read-write INI file is actually a value that reads and writes the corresponding key in a section, which can be done with just a few functions.


I. Functions for writing information to INI files
1. Write information to the system's Win.ini file
BOOL writeprofilestring (
LPCTSTR Lpappname,//The name of the section, is a string ending with 0
The name of the LPCTSTR Lpkeyname,//Key, is a string ending with 0. If null, the entire section is deleted
LPCTSTR the value of the lpstring//key, which is a string ending with 0. If NULL, the corresponding key is deleted
)


2. Write the information to your own defined. ini file
BOOL WritePrivateProfileString (
LPCTSTR Lpappname,//Ibid.
LPCTSTR Lpkeyname,//Ibid.
LPCTSTR lpstring,//Ibid.
LPCTSTR lpfilename//file name of the file to be written. If the INI file is in the same directory as the program, you can also use a relative
path, otherwise the absolute path needs to be given.
)

Such as:
:: WriteProfileString ("Test", "id", "Xym");
Create a test section in Win.ini and create a key ID in the section with a value of Xym

:: WritePrivateProfileString ("Test", "id", "Xym", "D:\\vc\\ex1\\ex1.ini");
Create a test section in Ex1.ini in the EX1 directory and create a key ID in the section with a value of Xym

If the Ex1.ini file is in the same directory as the program that reads and writes the file, the above statement can also be written as:
:: WritePrivateProfileString ("Test", "id", "Xym", ". \\ex1.ini");

It is important to note that in C-series languages, the escape character ' \ \ ' means a backslash ' \ '. Also, when using a relative path, the. number before \ \ is not lost.

second, the function of reading data from INI file
1. Read the information from the system's Win.ini file
(1) reading a string
DWORD GetProfileString (
LPCTSTR lpappname,//section name
LPCTSTR Lpkeyname,//Key name, reading the value of the key
LPCTSTR Lpdefault,//If the specified key does not exist, the value is the default value for the Read
LPTSTR lpreturnedstring,//A pointer to a buffer that receives the read string
DWORD nSize//Specifies the size of the buffer that the lpreturnedstring points to
)

Such as:
CString str;
:: GetProfileString ("Test", "id", "Error", str.) GetBuffer (20), 20);

(2) Read integer
UINT Getprofileint (
LPCTSTR Lpappname,//Ibid.
LPCTSTR Lpkeyname,//Ibid.
INT Ndefault//If the specified key name does not exist, the value is the default value for the Read
)

If the age information is written using the following statement:
:: WriteProfileString ("Test", "Age", "25");
Create a test section in Win.ini and create a key age in the section with a value of 25

You can use the following statement to read the value of the age key:
int age;
Age=::getprofileint ("Test", "age", 0);

2. Read the information from your own INI file
(1) reading a string
DWORD GetPrivateProfileString (
LPCTSTR Lpappname,//same 1 (1)
LPCTSTR Lpkeyname,//same 1 (1)
LPCTSTR Lpdefault,//same 1 (1)
LPTSTR lpreturnedstring,//same 1 (1)
DWORD nSize,//Same as 1 (1)
LPCTSTR lpFileName//Read the file name of the information. If the INI file is in the same directory as the program, you can also use the
To the path, otherwise the absolute path needs to be given.
)

Such as:
CString str;
:: GetPrivateProfileString ("Test", "id", "Error", str.) GetBuffer (+), ". \\ex1.ini");
Or:
:: GetPrivateProfileString ("Test", "id", "Error", str.) GetBuffer (+), "D:\\vc\\ex1\\ex1.ini");

(2) Read integer

UINT Getprivateprofileint (
LPCTSTR Lpappname,//Ibid.
LPCTSTR Lpkeyname,//Ibid.
INT Ndefault,//If the specified key name does not exist, the value is the default value for the Read
LPCTSTR lpFileName//Ibid.
)

If the age information is written using the following statement:
:: WritePrivateProfileString ("Test", "Age", "+", ". \\ex1.ini");
Create a test section in Ex1.ini and create a key age in the section with a value of 25

You can use the following statement to read the value of the age key:
int age;
Age=::getprivateprofileint ("Test", "age", 0, ". \\ex1.ini");

Third, delete the key value or the section

Review the description of the WriteProfileString function
BOOL writeprofilestring (
LPCTSTR Lpappname,//The name of the section, is a string ending with 0
The name of the LPCTSTR Lpkeyname,//Key, is a string ending with 0. If null, the entire section is deleted
LPCTSTR the value of the lpstring//key, which is a string ending with 0. If NULL, the corresponding key is deleted
)

Thus, to delete a section, you only need to set the second parameter of writeprofilestring to NULL. To delete a key, you only need to set the third argument of the function to null. This is the deletion of the section or key in the system's Win.ini, similarly, to delete the section or key in its own defined INI file, you can do the same.
Such as:
:: WriteProfileString ("Test", null,null); Delete the test section in Win.ini
:: WriteProfileString ("Test", "id", NULL); Delete the ID key in Win.ini

:: WritePrivateProfileString ("Test", Null,null, ". \\ex1.ini"); Delete the test section in Ex1.ini
:: WritePrivateProfileString ("Test", "id", NULL, ". \\ex1.ini"); Delete the ID key in Ex1.ini

Iv. How to determine how many sections in an INI file
The simplest way to determine how many sections are in an INI file is to find all the section names and then count the number of section names. To find out all the section names, use the Getprivateprofilesectionnames function to do so, and the prototype is as follows:
DWORD Getprivateprofilesectionnames (
LPTSTR Lpszreturnbuffer,//points to a buffer that holds all the section names returned
DWORD nSize,//size of parameter Lpszreturnbuffer
LPCTSTR lpfilename//file name, if the INI file and the program in the same directory,

You can also use a relative path, otherwise you need to give the absolute path
)

The following is a function to count the total number of sections in an INI file, and of course, if you need to find each key and its value in each section at the same time, it can be easily obtained by finding the section name.





int Ctestdlg::calccount (void)
{
TCHAR chsectionnames[2048]={0}; A character array that consists of all section names
char * PSECTIONNAME; Save the first address of a found section name string
int i; I point to a position of the array chsectionnames, starting with 0, moving back in order
int j=0; J Position offset for the first address of the next section name string relative to the current I
int count=0; Number of statistical sections

CString name;
Char id[20];
:: Getprivateprofilesectionnames (chsectionnames,2048, ". \\ex1.ini");
for (i=0;i<2048;i++,j++)
{
if (chsectionnames[0]== ')
Break If the first character is 0, then a section in the INI is not
if (chsectionnames[i]== ')
{
psectionname=&chsectionnames[i-j]; To find a 0, it means moving forward from this character, minus the J offset,
is the first address of a section name.

J=-1; After a section name is found, the value of J is restored to count the offset of the next section name address
1 is because the last character of the section name string, 0, is a terminator and cannot be used as a section name

Part of

You can get the value of the key in the section when you get the section name, provided that we know which keys are in that section.

AfxMessageBox (Psectionname); To show the found out.

if (chsectionnames[i+1]==0)
{
Break When two adjacent characters are 0 o'clock, all the section names are found and the loop terminates
}
}

}

return count;
}
Add Four points:

The path to the 1.INI file must be complete, the directory must exist before the file name, or the write is unsuccessful, and the function returns a value of FALSE.

2. The path of the file name must be \ \, because in VC + +, \ \ only represents a \.

3. The INI file can also be placed in the directory where the program is located, at this time the lpFileName parameter is: ". \\student.ini".

VC operation on INI file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.