Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20699
  • 博文数量: 11
  • 博客积分: 91
  • 博客等级: 民兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-02 16:14
文章分类
文章存档

2016年(11)

我的朋友
最近访客

分类: C/C++

2016-07-01 16:03:43

/*

功能:读取和写入ini文件

环境:VS2013  VC++

时间:2016-06-28

作者:Linkun

*/

//获取路径,生成ini文件名

       CString strPath, strFileName;

       strFileName = "test.ini";


       TCHAR _szPath[MAX_PATH+1] ={0};

       GetModuleFileName(NULL, _szPath, MAX_PATH);

       (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名 


       for (int n = 0; _szPath[n]; n++){

              if (_szPath[n] != _T('\\')){

                     strPath += _szPath[n];

              }

        else{

                     strPath += _T("\\\\");

              }


       }

       GetDlgItem(IDC_EDIT3)->SetWindowTextW(strPath);


       strPath = strPath + strFileName;


//ini文件操作

       CString strValue, strSection, strSectionKey;

//写入

       strSection = "Section1";

       strSectionKey = "Item1";


       GetDlgItemText(IDC_EDIT1, strValue);               //获取第1个文本框内容

       ////写入ini文件对应的键值

       WritePrivateProfileString(strSection, strSectionKey, strValue, strPath);  


//读取

       TCHAR strBuff[256];

       strValue = "";

       GetPrivateProfileString(strSection, strSectionKey, NULL, strBuff, 80, strPath);

       strValue = strBuff;

       GetDlgItem(IDC_EDIT2)->SetWindowTextW(strValue);  //读取的值写入第二个文本框



阅读(2473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~