Console环境下的ini文件的读写

ini文件的读写问题,搞了一下午终于弄好了,纠结呀~~~~~ios

     通常ini文件读写都是在mfc中完成的,此次要在Console环境中实现。在网上找了很久,都是东抄西抄的,都同样。。。并且都是在mfc下实现的,看来写东西的人愈来愈少了,让人心寒呀,最可悲的是,转载人家的还大言不惭的写着原创。nm都这么巧,跟人家创的同样。。。。。。废话很少说了。windows

    好了,如今把本身学到的ini读写与你们分享下。ide

    在Console环境中若是头文件写入#include "afx.h";程序会报错,这里须要设置下编译器,我用的是vs2008,项目-属性-常规-mfc的使用,使用mfc。如图:spa

以后再加入#include "afx.h";头文件就没问题了;下面把代码给你们分享:字符串

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

#include "stdafx.h"
#include <iostream>
#include "afx.h"
#include <string>
#include <windows.h> 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//写入字符串
	WritePrivateProfileString(_T("StudentInfo"),_T("Name"),_T("leeboy"),_T("d:\\student.ini")); 
	
	//数字也以字符串的形式写入
	WritePrivateProfileString(_T("StudentInfo"),_T("Age"),_T("22"),_T("d:\\student.ini")); 
	
	//读取字符串
	CString strStudName;
	GetPrivateProfileString(_T("StudentInfo"),_T("Name"),_T("默认姓名"),
		strStudName.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\student.ini")); 

	//读取整数
	int Result = GetPrivateProfileInt(_T("StudentInfo"),_T("Age"),0,_T("d:\\student.ini"));

	wcout << (LPCTSTR)strStudName << endl;//Console环境中CString的输出
	//wcout << strStudName.GetString() << endl;
	cout << Result;//输出数字
	system("pause");
	return 0;
}

 

只要组名如StudentInfo不一样便可连续写入
 编译器

相关文章
相关标签/搜索