C++如何读取txt文件

来源:百度知道 编辑:UC知道 时间:2024/06/01 23:00:57
[坦克将要走的位置]
Mesh=3
mesh_0X=12
mesh_0Y=14
mesh_0Z=-444
mesh_1X=9
mesh_1Y=14
mesh_1Z=-440
mesh_2X=12
mesh_2Y=14
mesh_2Z=-449

如何读取这些坐标值呢?
请详细说明下。。

#include <windows.h>
#include <strsafe.h>

...

int nMesh = GetPrivateProfileInt(_T("坦克将要走的位置"), _T("Mesh"), 0, _T("配置文件.ini"));
for(int i=0; i<nMesh; i++)
{
TCHAR szKey[128] = _T("");

StringCbPrintf(szKey, sizeof(szKey), _T("mesh_%dX"), i);
int x = GetPrivateProfileInt((_T("坦克将要走的位置"), szKey, 0, _T("配置文件.ini"));

StringCbPrintf(szKey, sizeof(szKey), _T("mesh_%dY"), i);
int y = GetPrivateProfileInt((_T("坦克将要走的位置"), szKey, 0, _T("配置文件.ini"));

StringCbPrintf(szKey, sizeof(szKey), _T("mesh_%dZ"), i);
int z = GetPrivateProfileInt((_T("坦克将要走的位置"), szKey, 0, _T("配置文件.ini"));

// 处理
}

lz的这个完全是key和value对
建议保存为ini文件
这样的话可以使用GetPrivateProfileSectionNames方法来取的对应的区块下的键值串,之后自己处理就可以了
txt的话
格式化读取没有什么特别现成可用的方法