Third: In the VC + + read and write INI file __c++

Source: Internet
Author: User
Tags function prototype

Read and write INI file in VC + +

Write INI file
CString Path,index;
int i;
Path= "d:/qc_download/bin_code/teu800_long_sms/";
I=1;
Index. Format (_t ("%d"), I);
:: WritePrivateProfileString (_t ("Successcompath"),
index,_t ("d:/qc_download/bin_code/teu800_long_sms/"), _t (".//ex1.ini"));

The information in the INI file is this:

[Successcompath]
0=d:/qc_download/bin_code/teu800_long_sms

such as: WritePrivateProfileString (_t ("FileCount"), _t ("Count"), _t ("Lijiandong"), _t ("D://usefile.ini"));

That's what happened in D://usefile.ini.

[FileCount]
Count=lijiandong

Read
CString Repath,reindex;
Int J;
:: GetPrivateProfileString (_t ("Successcompath"), _t ("0"), _t ("c://"),
Repath. GetBuffer (MAX_PATH), max_path,_t ("E://mfcdown//qdownload//qdownload//ex1.ini"));
J=atoi (Reindex);

Read the information from the Successcompath to the Repath. Read "c://" to Repath if no information is read

In our program, there are always some configuration information needs to be saved, in order to complete the function of the program, the easiest way is to write this information in the INI file, the program is initialized to read again. The specific application is as follows:

I. Write information to the. ini file.

1. The WINAPI function prototype used is:

BOOL WritePrivateProfileString (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
LPCTSTR lpstring,
LPCTSTR lpFileName
);

The meaning of each of these parameters:

LPCTSTR Lpappname is a field name in the INI file.

LPCTSTR Lpkeyname is a key name under Lpappname, popularly speaking is variable name.

LPCTSTR lpstring is a key value, which is the value of a variable, but must be of type LPCTSTR or CString.

LPCTSTR lpFileName is the full INI file name.

2. Specific use: the establishment of a current student, the need to put his name and age into the C:/stud/student.ini file.

CString strname,strtemp;
int nage;
Strname= "John";
nage=12;
:: WritePrivateProfileString ("Studentinfo", "Name", StrName, "C://stud//student.ini");

The contents of this C:/stud/student.ini file are as follows:

[Studentinfo]
Name= John

3. To save the student's age, simply change the integer value to a character type:

Strtemp.format ("%d", nage);
:: WritePrivateProfileString ("Studentinfo", "Age", strtemp, "C://stud//student.ini");

Two. Read the information from the INI file into the variables in the program.

1. The WINAPI function prototype used is:

DWORD GetPrivateProfileString (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
LPCTSTR Lpdefault,
LPTSTR lpreturnedstring,
DWORD Nsize,
LPCTSTR lpFileName
);

The meaning of each of these parameters:

The first two parameters are the same as those in writeprivateprofilestring.

Lpdefault: If the INI file does not have the field names or key names specified in the first two parameters, this value is assigned to the variable.

Lpreturnedstring: The CString object that receives the value in the INI file, that is, the destination buffer.

Nsize: The size of the destination cache.

lpFileName: Is the full INI file name.

2. How to use: Now to read the students ' information written in the previous step into the program.

CString Strstudname;
int nstudage;
GetPrivateProfileString ("Studentinfo", "name", "Default name", Strstudname.getbuffer (MAX_PATH), MAX_PATH, "c://stud// Student.ini ");

After execution Strstudname value is: "John", if the first two parameters are incorrect, the value is: "Default name."

3. Read the integer value to use another WINAPI function:

UINT Getprivateprofileint (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
INT Ndefault,
LPCTSTR lpFileName
);

The parameters here are the same. Use the following methods:

Nstudage=getprivateprofileint ("Studentinfo", "Age", "C://stud//student.ini");


Three. Loop write multiple values, set up an existing program to save a few recently used file names, the specific procedures are as follows:

1. Write:

CString Strtemp,strtempa;
int i;
int ncount=6;
file://a total of 6 file names need to be saved
For (I=0;i {strtemp.format ("%d", I);
strtempa= filename;
file://file names can be obtained from arrays, list boxes, and so on.
:: WritePrivateProfileString ("Usefilename", "FileName" +strtemp,strtempa,
"C://usefile//usefile.ini");
}
Strtemp.format ("%d", ncount);
:: WritePrivateProfileString ("FileCount", "Count", strtemp, "C://usefile//usefile.ini");
file://writes the total number of files so that they can be read out.

2. Read out:

Ncount=::getprivateprofileint ("FileCount", "Count", 0, "C://usefile//usefile.ini");
For (I=0;i {strtemp.format ("%d", I);
strtemp= "FileName" +STRTEMP;
:: GetPrivateProfileString ("Currentini", strtemp, "Default.fil", Strtempa.getbuffer (MAX_PATH), MAX_PATH, "c:// Usefile//usefile.ini ");

FILE://uses the content in Strtempa.

}

Add Four points:

The path to the 1.INI file must be complete, the level directory preceding the file name must exist, or the write will not succeed, and the function returns a value of FALSE.

2. The path to the filename must be//, because in VC + +,//only to represent one/.

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

4. Paste source code from the Web page, it is best to paste into Notepad, and then to the VC paste, otherwise easily caused by compiling errors, I was very puzzled at the beginning, good code is not it? later found this method. And some of the code uses Full-width Furu: <,\, ETC., will
caused a compilation error.

Example

//write INI file
 cstring strroot1
 strroot. Empty ();
 cstring curstr;//Current path
 int Curi;
 curi=d_combopath. GetCurSel ()//Gets the current index value,
 d_combopath. Getlbtext (CURI,CURSTR)//Get Combox content is stored in Curstr
 strroot1. Append (CURSTR);
     CString Index;
     int idx;
     idx=0;
     Index. Format (_t ("%d"), IDX);
    :: WritePrivateProfileString (_t ("Successcompath"),
       index,strroot1,_t (".//ex1.ini"));

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.