博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC Ini文件处理
阅读量:4028 次
发布时间:2019-05-24

本文共 2694 字,大约阅读时间需要 8 分钟。

//对ini文件的处理一例!  

  对.ini文件的操作  
  例如我的Config.ini文件如下:  
  [Config]  
  TCP   Host=255.0.0.1  
  TCP   Port=8888  
  UDP   Port=8200          
   
  其读写操作可以如下:  
  int   k;  
  char   linebuf[30];  
  char   workdir[64],sFileN[100];  
  getcwd(workdir,50);                       //获得当前运行文件路径       //用getcwd需#include   "direct.h"  
  wsprintf(sFileN,"%s//config.ini",(LPSTR)workdir);     //若用GetCurrentDirectory()则不需  
  int   m_tcpport=GetPrivateProfileInt("Config",   "TCP   Port",   0,   sFileN);  
  int     m_udpport=GetPrivateProfileInt("Config",   "UDP   Port",   0,   sFileN);    
  GetPrivateProfileString   ("Config",   "TCP   Host",   NULL,   linebuf,   99,   sFileN);  
  //读出了数据到linebuf  
  //以下是往.ini文件中写入数据  
  WritePrivateProfileString("Config","TCP   Port","8888",sFileN);  
  WritePrivateProfileString("Config","TCP   Host","255.0.0.1   ",sFileN);  
   
  附录:  
  BOOL   WritePrivateProfileString(  
      LPCTSTR   lpAppName,     //   section   name  
      LPCTSTR   lpKeyName,     //   key   name  
      LPCTSTR   lpString,       //   string   to   add  
      LPCTSTR   lpFileName     //   initialization   file  
  );  

 

 

 

 

 

给你一个“性感”类:  
    

 #ifndef   INIFILE_H  
  #define   INIFILE_H  
  class   CIniFile  
  {  
  public:  
  CIniFile();  
  virtual   ~CIniFile();  
   
  //   Operations  
  public:  
  BOOL   SetProfileInt(CString   pParent,   CString   pPrivateName,   int   pValue);  
  BOOL   SetProfileString(CString   pParent,   CString   pPrivateName,   CString   pValue);  
  int   GetProfileInt(CString   pParent,   CString   pPrivateName);  
  CString   GetProfileString(CString   pParent,   CString   pPrivateName);  
   
  protected:  
  private:  
  CString   fileName;  
  };  
   
  #endif  
   
  //cpp  
   
  #include   "stdafx.h"  
  #include   "IniFile.h"  
   
  CIniFile::CIniFile()  
  {  
  fileName   =   _T("");  
  TCHAR   exeFullPath[MAX_PATH];  
  GetModuleFileName(NULL,   exeFullPath,   MAX_PATH);  
   
  CString   tmp;  
  tmp.Format("%s",   exeFullPath);  
    int   ret   =   tmp.Find("//",   0);  
  while   (ret   !=   -1)  
  {  
  fileName   +=   tmp.Left(ret   +   1);  
  CString   pp;  
  pp   =   tmp.Right(strlen(tmp)   -   ret   -   1);  
  tmp   =   pp;  
  ret   =   tmp.Find("//",   0);  
  }  
  fileName   +=   "main.ini"; //配置文件名  
   
  CFile   file;  
  if   (!file.Open(fileName,   CFile::modeRead,   NULL))  
  {  
  file.Open(fileName,   CFile::modeCreate   |   CFile::modeWrite,   NULL);  
  char   pBuf[]   =   "";  
  file.Write(pBuf,   sizeof(pBuf));  
  }  
  file.Close();  
  }  
   
  CIniFile::~CIniFile()  
  {  
  }  
   
  CString   CIniFile::GetProfileString(CString   pParent,   CString   pPrivateName)  
  {  
  CString   tmp;  
  GetPrivateProfileString(pParent,   pPrivateName,   "",   tmp.GetBuffer(MAX_PATH),   MAX_PATH,   fileName);  
  tmp.ReleaseBuffer();  
  return   tmp;  
  }  
   
  int   CIniFile::GetProfileInt(CString   pParent,   CString   pPrivateName)  
  {  
  int   pValue;  
  pValue   =   GetPrivateProfileInt(pParent,   pPrivateName,   0,   fileName);  
  return   pValue;  
  }  
   
  BOOL   CIniFile::SetProfileString(CString   pParent,   CString   pPrivateName,   CString   pValue)  
  {  
  return   WritePrivateProfileString(pParent,   pPrivateName,   pValue,   fileName);  
  }  
   
  BOOL   CIniFile::SetProfileInt(CString   pParent,   CString   pPrivateName,   int   pValue)  
  {  
  CString   tmp;  
  tmp.Format("%d",   pValue);  
  return   WritePrivateProfileString(pParent,   pPrivateName,   tmp,   fileName);  
  }    

转载地址:http://zyobi.baihongyu.com/

你可能感兴趣的文章
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
部分笔试算法题整理
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
2017阿里内推笔试题--算法工程师(运筹优化)
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>