文件读取小问题

chengliang80 2002-05-15 10:40:25
我要向一个文件中写入一个变量,例如money=999,写入的时候写入money=999,但读出的时候,读出999。我该怎么写入,怎么读出,麻烦写的详细点,我是个菜鸟。
...全文
26 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcman1990 2002-05-15
  • 打赏
  • 举报
回复
用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);
lightrain 2002-05-15
  • 打赏
  • 举报
回复
太详细了,不大好吧

^_^
aicai 2002-05-15
  • 打赏
  • 举报
回复
自己试一试把!!!!
CIniFile Ini("个人计数器.ini");
中的“个人计数器.ini”是文件名!!!!
aicai 2002-05-15
  • 打赏
  • 举报
回复
.h文件
// 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 -1

class 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);
nne998 2002-05-15
  • 打赏
  • 举报
回复
用ini文件吧。

参考delphi 有关 "TiniFile" 的帮助

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧