读取属性文件

在这里碰到在CString中显示中文出现乱码,但CString完全可以识别unicode字符集。

考虑良久,发现是在_tprintf()中的问题,如果按书中printf()显示则会是乱码,但如果_tprintf()的话,前面“”号中又不能转化宽字符,所以加上_T来显性表示

如果想用到mfc中的cstring,在工程建立下,没有包含mfc头文件的话,可以在项目属性下-》常规下-》选择在静态库中使用MFC。在加上头文件#include <afx.h>就ok

以下是代码:

//CProperties.h

#pragma  once

#include "CProperty.h"
#include <fstream>
#include <string>
#include <iostream>


using namespace std;

class CProperties
{
private:
	CMapStringToPtr _map;

public:
	CProperty * getProperty(CStringW name);
	~CProperties();
	int loadFrom(ifstream& in);
};

//CProperty.h

#include <afx.h>

class CProperty
{
private:
	 CString _value;
public:
	CProperty(CString value);
	CString getString(void);
	int getInteger(void);
	bool getBoolean(void);
};

//CProperties.cpp

#include "StdAfx.h"
#include "CProperties.h"



using namespace std;

CProperty * CProperties::getProperty(CString name)
{
	void * pa;
	if(_map.Lookup(name, pa))
		return (CProperty *)pa;

	return NULL;
}

CProperties::~CProperties(void)
{
	POSITION pos;
	CString key;
	void * pa;

	for (pos = _map.GetStartPosition(); pos != NULL;)
	{
		_map.GetNextAssoc(pos, key, pa);
		delete pa;
	}
}

int CProperties::loadFrom(ifstream & in)
{
	int lines = 0;
	
	
	while(!in.eof())
	{
		//读取其中的一行
		char line[256];
		in.getline(line, 255);

		string s = line;

		//空白行,跳过
		if(s.empty())
			continue;

		//#为注释标记,跳过
		if(s[0] == '#')
			continue;

		//不包含=,跳过
		int i = s.find("=");
		if(i < 0)
			continue;

		//拆分成key=value
		string key = s.substr(0, i);
		string value = s.substr(i + 1);
		
		_map.SetAt(CString(key.c_str()), new CProperty(CString(value.c_str())));

		lines++;
	}

	return lines;
}

//CProperty.cpp

#include "stdafx.h"
#include "CProperty.h"


CProperty::CProperty(CString value)
{

	_value = value;
}

CString CProperty::getString(void)
{
	return _value;
}

int CProperty::getInteger(void)
{
	return _wtoi(LPCTSTR(_value));
}

bool CProperty::getBoolean(void)
{
	return CString("true").CompareNoCase(_value) == 0;
}

// PropertiesReader.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "CProperties.h"
//#include <locale.h>
//#include <string>
//
//using namespace std;
//
//string wideChar2String(const CString& cstr)
//{
//	int n = cstr.GetLength();
//	int len = WideCharToMultiByte(CP_UTF8, 0, cstr, cstr.GetLength(), NULL, 0, NULL, NULL);
//	char* str = new char[len + 1];
//	WideCharToMultiByte(CP_UTF8, 0, cstr, cstr.GetLength()+1, str, len+1, NULL, NULL);
//	str[len] = '\0';
//	string sStr(str);
//	delete [] str;
//
//	return sStr;
//}



int _tmain(int argc, _TCHAR* argv[])
{
	setlocale(LC_ALL, "chs");

	CProperties ps;
	ifstream in("C:\\Users\\sony\\Desktop\\sample.properties");
	ps.loadFrom(in);

	CString hotelName = ps.getProperty("hotelName")->getString();
	CString boss = ps.getProperty("boss")->getString();
	int maxClients = ps.getProperty("maxClients")->getInteger();
	bool isOpen = ps.getProperty("isOpen")->getBoolean();

	_tprintf(_T("酒店: %s\r\n老板: %s\r\n最大顾客数: %d\r\n状态: %s\r\n"), hotelName, boss, maxClients, isOpen ? _T("开张") : _T("打烊"));

	


	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值