用ini文件吧。参考delphi 有关 "TiniFile" 的帮助
解决方案 »
- UDP只允许一包一包接收,还是可以调用recvfrom一次接收多个包?
- 怎么把CRicheditctrl的内容输出到指定区域
- WINDOWS如何创建连接来设置模块路径
- 在视图中的某一部分画线,视图的其它部分无法画线,应该怎麽做?
- nResponse的值应该怎样使他不一样啊?
- 初学Direct3D,请教一个很基础的问题。
- 命令行程序中是否不能使用ADO连接SQLServer数据库?急死了!
- 特急:如何用一幅真彩jpg图片作为Dialog的背景图片,并且该图片要保存在资源文件中。请高手指点,百分相送,多谢了!
- 如何像 WINRAR 那样,既得到了目录名,也得到了文件名
- 迫切需要您的帮助和提示
- 在VB中做的OCX控件能否放到VC中调用?
- 进行过磁盘编程的请进
// IniFile.h: interface for the CIniFile class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_INIFILE_H__2DC55044_7053_11D5_BE46_00A024D33B00__INCLUDED_)
#define AFX_INIFILE_H__2DC55044_7053_11D5_BE46_00A024D33B00__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define FunOk 0;
#define FunFail -1class CIniFile
{
public:
int WriteInteger(LPCTSTR SectionName, LPCTSTR KeyName, long KeyValue);
int WriteString(LPCTSTR SectionName, LPCTSTR KeyName, LPCTSTR KeyValue);
void ReadInteger(LPCTSTR SectionName, LPCTSTR KeyName, long &KeyValue);
void ReadString(LPCTSTR SectionName ,LPCTSTR KeyName ,CString &KeyValue);
CIniFile(LPCTSTR FileName);
virtual ~CIniFile();private:
CString sFileName;
};#endif // !defined(AFX_INIFILE_H__2DC55044_7053_11D5_BE46_00A024D33B00__INCLUDED_)
.cpp文件
// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
//#include "个人计算器.h"
#include "IniFile.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CIniFile::CIniFile(LPCTSTR FileName)
{
//sFileName = FileName;
/*if(sFileName.Find('\\') < 0 )
{
char sOldDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH ,sOldDir );
sFileName = sOldDir;
sFileName +="\\";
sFileName +=FileName;
}*/
TCHAR sFilename[_MAX_PATH];
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFname[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];
GetModuleFileName(AfxGetInstanceHandle(),sFilename,_MAX_PATH); _tsplitpath(sFilename, sDrive ,sDir ,sFname ,sExt);
CString homeDir(CString(sDrive ) + CString(sDir));
int nLength = homeDir.GetLength();
if( homeDir.GetAt(nLength - 1) != _T('\\'))
homeDir +=_T("\\");
sFileName = homeDir;
sFileName +=FileName;
}CIniFile::~CIniFile()
{
sFileName ="";}void CIniFile::ReadString(LPCTSTR SectionName, LPCTSTR KeyName, CString &KeyValue)
{
char * result = new char[300];
int Totalsize = 300 ;
GetPrivateProfileString( SectionName , KeyName , KeyValue ,result ,Totalsize ,sFileName);
KeyValue = result ;
delete [] result;}void CIniFile::ReadInteger(LPCTSTR SectionName, LPCTSTR KeyName, long &KeyValue)
{
char *result = new char[300];
int Totalsize = 300 ;
CString StrKeyValue;
StrKeyValue.Format("%d",KeyValue);
GetPrivateProfileString( SectionName ,KeyName ,StrKeyValue, result , Totalsize , sFileName);
if( result != "" )
KeyValue = atoi( result );
delete [] result;
}int CIniFile::WriteString(LPCTSTR SectionName, LPCTSTR KeyName, LPCTSTR KeyValue)
{
if( WritePrivateProfileString( SectionName , KeyName ,KeyValue ,sFileName) !=0)
{ return FunOk;}
else
return FunFail;
}int CIniFile::WriteInteger(LPCTSTR SectionName, LPCTSTR KeyName, long KeyValue)
{
char tempkey[24];
CString temps;
_itoa( KeyValue ,tempkey , 10 );
temps = tempkey;
if( WritePrivateProfileString( SectionName , KeyName ,temps ,sFileName) !=0)
{return FunOk;}
else
return FunFail;
}例子:::
读
CIniFile Ini("个人计数器.ini"); Ini.ReadInteger("Noon","Noon hour",(long &)m_nnhour);
Ini.ReadInteger("Noon","Noon minute",(long &)m_nnminute);
Ini.ReadInteger("Noon","Noon second",(long &)m_nnsecond);写
CIniFile Ini("个人计数器.ini");
Ini.WriteInteger("Color","background color",bgcolor);
Ini.WriteInteger("Color","edit frame color", editcolor);
CIniFile Ini("个人计数器.ini");
中的“个人计数器.ini”是文件名!!!!
CString strFileName = _T("somefile.ini);
CString strSec = _T("variables");
CString strKey = _T("money");
int nMoney = 999;写入时:
CString strMoney;
strMoney.Format(_T("%d"), nMoney);
WritePrivateProfileString(strSec, strKey, strMoney, strFileName);读取时:
如果变量是数字:
UINT nMoneyRead = GetPrivateProfileInt(strSec, strKey, 0, strFileName);
如果是字符串:
CHAR szMoney[128];
GetPrivateProfileString(strSec, strKey, _T(""), szMoney, sizeof(szMoney)/sizeof(CHAR), strFileName);