VC中用函数读写ini文件的实例

 

#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <atlstr.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 CString strName;
CString strTemp;
int nAge;
strName = "zhangsan";
nAge = 12;
   
/* 
*
此函数功能是向文件
stud.ini
文件里写入一段配置项记录:
 
*
其中配置段项名称为
 "StudentInfo"
;配置项一个名称为
Name;
*
其值为
CString
类型变量
strName
所指的内容
 
*/
::WritePrivateProfileString("StudentInfo","Name","000000","d:\\stud.ini");
/* 
*
此函数功能是向文件
stud.ini
文件里写入一段配置项记录:
 
*
其中配置段项名称为
 "StudentInfo"
;配置项一个名称为
Age;
*
其值为
int
类型变量
nAge
所指的内容
,
只是之前需要转换成
CString
类型
 
*
最后要指定写文件的路径和名称
 
*/
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"d:\\stud.ini");
/*
*
以下为读出,读出
CString
类型和
int
类型的方法不一样。函数的具体的功能可以查看
MSDN
*/
CString strStudName;
int nStuAge;
::GetPrivateProfileString("StudentInfo","Name","defualt",
strStudName.GetBuffer(MAX_PATH),
MAX_PATH,"d:\\stud.ini");
nStuAge = ::GetPrivateProfileInt("StudentInfo","Age",10,"d:\\stud.ini");
cout<<"strStudName = "<< strStudName <<endl;
cout<<"nStuAge = "<< nStuAge <<endl;
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值