首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

读取INI文件,读出来的是异常的值

2012-02-09 
读取INI文件,读出来的是错误的值Debug下有内容为以下的Play.ini文件[PlayLyric]left612top184right882b

读取INI文件,读出来的是错误的值
Debug下有内容为以下的Play.ini文件

[PlayLyric]
left=612
top=184
right=882
bottom=644
[PlayerList]
left=342
top=352
right=612
bottom=644

char   apppath[256];

GetCurrentDirectory(256,apppath);
CString   path;
path.Format( "%s ",apppath);
path+= "Play.ini ";
this-> m_left   =   GetPrivateProfileInt( "PlayerList ", "left ",this-> m_left,path);
this-> m_top   =   GetPrivateProfileInt( "PlayerList ", "top ",this-> m_top,path);
this-> m_right=   GetPrivateProfileInt( "PlayerList ", "right ",this-> m_right,path);
this-> m_bottom=   GetPrivateProfileInt( "PlayerList ", "bottom ",this-> m_bottom,path);

读出来后m_left竟然为1380,根本就不是342.m_top为1498,不是352。
m_left默认值为324,m_top默认值为352


[解决办法]
看你贴的没问题,其它肯定有影响
[解决办法]
代码本身没有什么问题
GetCurreentDirectory()得到的路径是否正确?
[解决办法]
应该是读数据的时候读错位了。
[解决办法]
在获取文件路径上有问题.GetCurrentDirectory(...)获得的路径并不一定是Play.ini的路径.
2,path+= "\\Play.ini ".
这里有一个调试通过的代码:
#include "stdafx.h "
#include <Windows.h>
//#include <afxstr.h>
#include <atlstr.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR apppath[256];
memset(apppath,0,256);
GetModuleFileName(NULL,apppath,255);//GetCurrentDirectory(256,apppath)

TCHAR* p = _tcsrchr(apppath, '\\ ');
p[1]=0;
//m_path = apppath;

CString path=apppath;
//path.Format( "%s ",apppath);
path+= "\\Play.ini ";
const CString pl= "PlayerList ";
const CString L= "left ";
int m_left = GetPrivateProfileInt(pl,L,1,path);
/*int m_top = GetPrivateProfileInt(pl, "top ",1,path);
int m_right= GetPrivateProfileInt(pl, "right ",1,path);
int m_bottom= GetPrivateProfileInt(pl, "bottom ",1,path);*/
printf( "%d ",m_left);
getchar();

return 0;
}
[解决办法]
GetPrivateProfileInt( "PlayerList ", "left ",this-> m_left,path);
GetPrivateProfileInt( "PlayerList ", "top ",this-> m_top,path);
GetPrivateProfileInt( "PlayerList ", "right ",this-> m_right,path);
GetPrivateProfileInt( "PlayerList ", "bottom ",this-> m_bottom,path);

直接这样写就没问题,至于你的那个,我想不出有什么问题

热点排行