⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 在vc下访问ini文件.htm

📁 这是关于在VC中应用MFC的简明教程的一组完整网页集合。特点:简明--既简洁又明了
💻 HTM
字号:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="file:///C|/Inetpub/wwwroot/XK/CSS/CSS1.css" rel="stylesheet" type="text/css">
</head>

<body>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<!--Fireworks MX 2004 Dreamweaver MX 2004 target.  Created Wed May 19 21:53:53 GMT+0800 2004-->
<link href="CSS/CSS1.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#ffffff">
<div align="center">
  <center>
<table border="0" cellpadding="0" cellspacing="0" width="760">
<!-- fwtable fwsrc="未命名" fwbase="top.gif" fwstyle="Dreamweaver" fwdocid = "1215258446" fwnested="0" -->
  <tr>
   <td><img src="images/top/spacer.gif" width="109" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="109" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="108" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="110" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="109" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="109" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="106" height="1" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="1" height="1" border="0" alt=""></td>
  </tr>

  <tr>
   <td colspan="7"><img name="top_r1_c1" src="images/top/top_r1_c1.jpg" width="760" height="125" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="1" height="125" border="0" alt=""></td>
  </tr>
  <tr>
   <td><a href="main.asp"><img name="top_r2_c1" src="images/top/top_r2_c1.gif" width="109" height="40" border="0" alt=""></a></td>
   <td><a href="newslist.asp?classid=1"><img name="top_r2_c2" src="images/top/top_r2_c2.gif" width="109" height="40" border="0" alt=""></a></td>
   <td><a href="xyjs.asp"><img name="top_r2_c3" src="images/top/top_r2_c3.gif" width="108" height="40" border="0" alt=""></a></td>
   <td><a href="szll.asp"><img name="top_r2_c4" src="images/top/top_r2_c4.gif" width="110" height="40" border="0" alt=""></a></td>
   <td><a href="newslist.asp?classid=5"><img name="top_r2_c5" src="images/top/top_r2_c5.gif" width="109" height="40" border="0" alt=""></a></td>
   <td><a href="newslist.asp?classid=3"><img name="top_r2_c6" src="images/top/top_r2_c6.gif" width="109" height="40" border="0" alt=""></a></td>
   <td><a href="ssjl.asp"><img name="top_r2_c7" src="images/top/top_r2_c7.gif" width="106" height="40" border="0" alt=""></a></td>
   <td><img src="images/top/spacer.gif" width="1" height="40" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="7"><img name="top_r3_c1" src="images/top/top_r3_c1.jpg" width="760" height="5" border="0" alt=""></td>
   <td><img src="images/top/spacer.gif" width="1" height="5" border="0" alt=""></td>
  </tr>
</table>
  </center>
</div>
</body>
</html>


<title>信息学院动态--在VC下访问INI文件</title>

<table width="760"  border="0" align="center" class="righttable" style=" word-break:break-all" cellpadding=3>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center" class="newsTopicTd">在VC下访问INI文件</td>
  </tr>
  <tr>
    <td align="center">2004-12-2 17:03:21</td>
  </tr>
  <tr>
    <td height="500" valign="top"><font size=+1>不论是程序开发人员还是软件应用人员,都不会对扩展名为“ini”的文件感到陌生,不仅Windows操作系统将大名鼎鼎的win.ini作为记录当前系统状态,并根据其记录内容对系统进行配置的一种便捷的方法,而且众多的应用软件也广泛地使用该类型的配置文件来对软件进行记录、配置。本文针对配置设置文件的使用进行讨论,以期能为软件状态的记录与设置寻求一种方便简洁的实现方法。&nbsp;<br><br>INI文件&nbsp;<br><br>配置设置(INI)文件是Windows操作系统中的一种特殊ASCII文件,以“ini”为文件扩展名。该文件也被称做是初始化文件(Initialization&nbsp;file)和概要文件(Profile),通常应用程序可以拥有自己的配置设置文件来存储状态信息。一般来说私有的配置设置文件比较小,这样可以减少程序在初始化时读取配置文件时的信息量,从而提高程序的启动速度、提高应用程序和系统的性能。如果待存取的信息涉及到Windows系统环境或是其他应用程序时,&nbsp;就必须在Windows系统的配置文件win.ini中记录并在访问的同时发送WM_WININICHANGE消息给所有的顶层窗口,通知其他的程序系统配置文件已做了更改。但由于win.ini中不仅记录了系统的有关信息,也存储着许多其他应用软件的配置数据,所以访问的数据量要远比私有的配置文件大得多。&nbsp;<br><br>配置文件里的信息之所以能为系统和众多的软件所读取并识别,是由于其内部对数据的存取采用了预先约定的“项-值对(entry-value&nbsp;pairs)”存储结构,&nbsp;并对待存取的数据分门别类地进行条理清晰的存储。下面是系统目录下win.ini文件的部分内容:&nbsp;<br><br>[windows]&nbsp;<br><br>load=&nbsp;<br><br>run=&nbsp;<br><br>NullPort=None&nbsp;<br><br>[Desktop]&nbsp;<br><br>WallpaperStyle=2&nbsp;<br><br>Pattern=(无)&nbsp;<br><br>配置文件把信息分成若干“节”,节标题放在方括号中,如“[Desktop]”就是Desktop节,在一个节内包含了一些与之相关的“项”,并通过等号对其进行赋值。一般的形式如下:&nbsp;<br><br>[SECTION]&nbsp;<br><br>ENTRY=VALUE&nbsp;<br><br>其中VALUE值可以有两种类型:&nbsp;数值型和字符串型。Windows操作系统专门为此提供了6个API函数来对配置设置文件进行读、写:&nbsp;<br><br>GetPrivateProfileInt()&nbsp;从私有初始化文件获&nbsp;<br><br>取整型数值&nbsp;<br><br>GetPrivateProfileString()&nbsp;从私有初始化文件获&nbsp;<br><br>取字符串型值&nbsp;<br><br>GetProfileInt&nbsp;从win.ini获取整数值&nbsp;<br><br>GetProfileString&nbsp;从win.ini获取字符&nbsp;<br><br>串值&nbsp;<br><br>WritePrivateProfileString&nbsp;写字符串到私有初始&nbsp;<br><br>化文件&nbsp;<br><br>WriteProfileString&nbsp;写字符串到win.ini&nbsp;<br><br>这里需要指出:&nbsp;当向配置文件存储信息时,不论是数据还是字符串都要先转换成字符串,然后再进行存储。&nbsp;<br><br>API函数的应用&nbsp;<br><br>按照通常的编程习惯,在程序刚开始运行时对配置设置文件进行读取访问,获取存储在其中的数据,并按照这些数据对程序进行配置,比如可以决定加载哪些组件、显示的位置等等。存储一般比较灵活,可以显式地由用户发送命令来执行,也可以在程序退出前把当前状态存储其内,以便下次运行时能恢复到上次退出时的界面。下面就通过几个程序片段对前面提到过的访问配置文件的API函数的使用进行介绍。&nbsp;<br><br>我们可以把视图类的OnInitialUpdate&nbsp;函数作为程序启动时读取配置文件的入口,配置文件的存储格式如下:&nbsp;<br><br>[SECTION&nbsp;1]&nbsp;<br><br>XPos=300&nbsp;<br><br>YPos=200&nbsp;<br><br>[SECTION&nbsp;2]&nbsp;<br><br>Text=Hello&nbsp;<br><br>这里仅有两个节,XPos和YPos标明了待显示信息的坐标,而待显示的信息存储在第二节的Text项中,用访问私有配置设置文件的API函数将其分别读入到变量m_nXPos、m_nYPos和m_strText中,并通过Invalidate调用OnDraw函数,在其内用TextOut函数将该信息在读取的坐标位置显示出来:&nbsp;<br><br>m_nXPos=GetPrivateProfileInt(&nbsp;<br><br>“SECTION&nbsp;1”,&nbsp;//节名&nbsp;<br><br>“XPos”,&nbsp;//项名&nbsp;<br><br>0,&nbsp;//没找到此项时的缺省返回值&nbsp;<br><br>“C:\\test\\debug\\test.ini”);&nbsp;<br><br>//配置文件的准确路径&nbsp;<br><br>m_nYPos=GetPrivateProfileInt(“SECTION&nbsp;1”,“YPos”,0,exeFullPath);&nbsp;<br><br>char&nbsp;buf[256];&nbsp;<br><br>len=GetPrivateProfileString(&nbsp;<br><br>“SECTION&nbsp;2”,&nbsp;//节名&nbsp;<br><br>“Text”,&nbsp;//项名&nbsp;<br><br>“No&nbsp;Text”,&nbsp;//没找到此项时的返回值&nbsp;<br><br>buf,&nbsp;//目标缓冲区地址&nbsp;<br><br>256,&nbsp;//目标缓冲区长度&nbsp;<br><br>“C:\\test\\debug\\test.ini”);&nbsp;<br><br>//配置文件的准确路径&nbsp;<br><br>for(int&nbsp;i=0;i&nbsp;CString&nbsp;str;&nbsp;<br><br>str.Format(“%c”,buf[i]);&nbsp;<br><br>m_strText+=str;&nbsp;<br><br>}&nbsp;<br><br>Invalidate();&nbsp;<br><br>一般配置文件是和应用程序存放在同一个目录中的,如果用“C:\\test\\debug\\test.ini”的绝对路径进行设置就会出现路径改变后找不到配置文件的问题,所以应动态搜寻配置文件的存放地址:&nbsp;<br><br>TCHAR&nbsp;exeFullPath[MAX_PATH];&nbsp;<br><br>//MAX_PATH在API中有定义,为128&nbsp;<br><br>int&nbsp;len=GetModuleFileName(NULL,&nbsp;<br><br>exeFullPath,&nbsp;//应用程序全路径存放地址&nbsp;<br><br>MAX_PATH);&nbsp;<br><br>CString&nbsp;path=“\\test.ini”;&nbsp;//配置文件名&nbsp;<br><br>::strcpy(exeFullPath+len-13,path);&nbsp;<br><br>//组合出配置文件的全路径&nbsp;<br><br>写配置文件也基本类似,只是需要把数值类型的变量格式化成字符串再行存储:&nbsp;<br><br>str.Format(“%d”,m_nXPos);&nbsp;<br><br>WritePrivateProfileString(“SECTION&nbsp;1”,“XPos”,str,exeFullPath);&nbsp;<br><br>str.Format(“%d”,m_nYPos);&nbsp;<br><br>WritePrivateProfileString(“SECTION&nbsp;1”,“YPos”,str,exeFullPath);&nbsp;<br><br>WritePrivateProfileString(“SECTION&nbsp;2”,“Text”,m_strText,exeFullPath);&nbsp;<br><br>这样的程序常遇到:&nbsp;在执行过一遍以后,重启系统会自动加载该程序,其实除了在启动菜单和注册表添加信息外,也可以用WriteProfileString函数向win.ini的“windows”节的“run”项目添加应用程序的全路径来实现,这要比其他两种方法简便得多,而且也比较安全。&nbsp;<br><br>实践中,采用ini配置设置文件是Windows下配置系统和应用软件的一种非常普遍的方法,合理地使用该配置文件,不仅能增强软件的自身功能,也可以加强同系统、其他应用软件的相互联系,使我们的程序更加灵活。&nbsp;<br><br></font></td>
  </tr>
  <tr>
    <td align="right">点击:364&nbsp;&nbsp;&nbsp;&nbsp;来源:未知 添加员:GMzATo</td> 
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="760"  border="0" align="center" class="righttable" style=" word-break:break-all">
  <tr>
    <td width="50%" word-break:break-all" valign=top>
    评论摘录:<br>
本文章当前没有评论
    </td>
    <td widht="50%" word-break:break-all" valign=top>
      <form action="savepl.asp" method="post">
        发表评论:<br>
        名字:<input name=username size=12 maxlength=20><br>
        <textarea cols="40" rows="7" name="content"></textarea><br>
        <input type=hidden name=newsid value=150>
        <input type=submit name=submit value="提 交"> 
      </form>   
    </td>
  </tr>
</table>

<br><center>
  <table width="775" border="0">
  <tr>
    <td><hr noshade color="#de8e08">
      <center>
        <p>版权所有 青岛科技大学 信息科学技术学院<br>
          联系方法:Tel 8958955 Email:Ist_qust@163.com<br>
            信息科学技术学院网络维护中心 <br>
          </p>
        </center></td>
  </tr>
</table>
</center>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -