一种简洁明了地读取文本文件的方法
2008-09-01 09:48
417 查看
利用函数GetPrivateProfileString (strSection,strSectionKey, NULL, intBuf, 99, strFilePath)
下面给一段例子程序
#include <iostream.h>
#include <afx.h>
void main()
{
int record_id,column_id,retcode;
char intBuf[16];
CString strSection;// = _T("");
CString strSectionKey;// = _T("");
CString strFilePath = ".//prc.ini";//这里.//不可少,否则读不到文件
strSection = "设备号";
strSectionKey = "record_id";
memset(intBuf ,0 ,sizeof(intBuf));
retcode = GetPrivateProfileString (strSection,strSectionKey, NULL, intBuf, 99, strFilePath);
if (retcode==NULL)
cout<<"error!"<<endl;
else
record_id = atoi(intBuf);
strSectionKey = "column_id";
memset(intBuf ,0 ,sizeof(intBuf));
retcode = GetPrivateProfileString (strSection,strSectionKey, NULL, intBuf, 99, strFilePath);
if (retcode==NULL)
cout<<"error!"<<endl;
else
column_id = atoi(intBuf);
cout<<record_id<<endl<<column_id<<endl;
}
在当前目录下新建一个名为prc.ini的文本文件,内容如下:
[设备号]
record_id=1986
column_id=5
运行程序结果为:
1986
5
下面给一段例子程序
#include <iostream.h>
#include <afx.h>
void main()
{
int record_id,column_id,retcode;
char intBuf[16];
CString strSection;// = _T("");
CString strSectionKey;// = _T("");
CString strFilePath = ".//prc.ini";//这里.//不可少,否则读不到文件
strSection = "设备号";
strSectionKey = "record_id";
memset(intBuf ,0 ,sizeof(intBuf));
retcode = GetPrivateProfileString (strSection,strSectionKey, NULL, intBuf, 99, strFilePath);
if (retcode==NULL)
cout<<"error!"<<endl;
else
record_id = atoi(intBuf);
strSectionKey = "column_id";
memset(intBuf ,0 ,sizeof(intBuf));
retcode = GetPrivateProfileString (strSection,strSectionKey, NULL, intBuf, 99, strFilePath);
if (retcode==NULL)
cout<<"error!"<<endl;
else
column_id = atoi(intBuf);
cout<<record_id<<endl<<column_id<<endl;
}
在当前目录下新建一个名为prc.ini的文本文件,内容如下:
[设备号]
record_id=1986
column_id=5
运行程序结果为:
1986
5
相关文章推荐
- 一种openresty在init_by_lua_file阶段读取redis到共享内存的方法
- 三版写文件的一种方法。TFileText,逐行读取数据。
- java properties 的六种读取方法 以及一种自己最方便的实现
- 简洁常用权限系统的设计与实现(七):一种错误的不可行的构造树的方法
- 一种从unicode文本中逐行读取字符串的方法
- java读取UTF-8文本文件第一个字符多出一个问号解决方法
- Android中读取文本文件中内容的方法
- 实现文件拖放的一种简洁方法
- 关于在虚拟机中无法读取usb设备的一种原因及处理方法
- Spring Batch读取csv文件失败的一种解决方法
- UNICODE环境下整行读取文本文件的方法
- 一种快速简洁解析JSON数据接口的方法
- 一种c#读取文本比较简单的方法
- 最简洁读取文件的方法
- C++从文本文件读取数据到vector中的方法
- Android-读取所有联系人的简洁方法
- C#中读取文本文件导入SQL数据库解决方法 推荐
- 资源文件读取的一种方法
- Delphi中读取文本文件的方法(实例一)
- 用vbs实现读取文本文件的方法