MFC读取txt文件问题

小宋朝 2014-04-14 07:04:15
这是txt文件内容的大概格式
; ID X Y B1 B2 B3
1 2730 1890 114 35 30
2 2731 1890 115 34 25
3 2732 1890 121 37 30
4 2729 1890 117 36 28
5 2730 1891 111 34 23
6 2731 1891 114 34 23
其中,B1、B2可以一直到BN(N不会很大,大概几百的样子),ID号也不是一定的。
现在的问题是,我想把这些参数都读进内存中,方便后续使用。但是,不知道如何把里面的单个数据取出来给对应的数据赋值,有谁有比较好的思路么?指点一二。
结构体如下;
struct data {
int ID;
int x;
int y;
vector<int> B;
};
list<data> data_list;
其实我就是卡在读取其中的一行,然后把改行的值给对应的data结构体赋值。
...全文
341 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
Liekkas 2014-04-15
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/350055569 当年可真是个小白啊
小宋朝 2014-04-15
  • 打赏
  • 举报
回复
引用 18 楼 mujiok2003 的回复:
[quote=引用 17 楼 xiaosongchao 的回复:] [quote=引用 16 楼 mujiok2003 的回复:] [quote=引用 15 楼 xiaosongchao 的回复:] 不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
std::wstring ---> openmode, 要干什么?[/quote]

m_file.ReadString(str);
	cout<<"Line Readed--> "<<str<<endl;
	wstring  str_s, cc;
	str_s = str.GetBuffer(0);
	istringstream str_roi(str_s);
	for (int i=0; i<(Bmax+3); i++)
	{
		str_roi>>cc;
		cout<<cc<<endl;
	}
	str.ReleaseBuffer();
其中,istringstream str_roi(str_s);这一句通过不了编译。。。怎么尽出一些我不熟悉的问题呢。。。。[/quote] 使用iwstringstream[/quote] 你的方法学习了,很不错。但是不能很好的适用于unicode字符集。多字节下可以直接使用。
小宋朝 2014-04-15
  • 打赏
  • 举报
回复
引用 21 楼 WUYUAN2011WOAINI 的回复:
http://www.jizhuomi.com/software/234.html 看看后面的 读写结构体
你的方法也不错,可惜不是很适合我。因为项目中不仅仅只有这些规律的字符串,还有其他的需要读。
小宋朝 2014-04-15
  • 打赏
  • 举报
回复
引用 19 楼 zhangyonghui2117 的回复:
思路:用到cstdiofile来读一行到cstring对象中,然后cstring转char* ,用字符串流来从字符中分离整数 下面代码测试通过,楼主应该会用,需包含头文件: #include <vector> #include <strstream> using namespace std;
// 结构体
struct data {
	int ID;
	int x;
	int y;
	vector<int> B;
};

// 获得信息到vec中
void GetData(vector<data>& vec,char* szFilePath){
	CStdioFile file;
	file.Open(szFilePath,CFile::modeReadWrite);// data.txt路径
	CString strLine;
	
	while(file.ReadString(strLine)) // 得到每一行数据
	{
		data T_data;
		int nValue,n=0;

		// LPCTSTR,多字节它为char* ,unicode它是wchar,此处参数为char*
		istrstream istr((LPCTSTR)strLine);
		while(istr>>nValue){
				n++;
			switch(n){
			case 1:
					T_data.ID = nValue;break;
			case 2:
					T_data.x = nValue;break;
			case 3:
					T_data.y = nValue;break;
			default:
				T_data.B.push_back(nValue);
				break;
			}
		}
		vec.push_back(T_data);
	}
}

// 测试显示数据是否正确
void CMfc001Dlg::OnOK() 
{
	// TODO: Add extra validation here
	vector<data> vec;
	GetData(vec,"I:\\data.txt");
	
	for (vector<data>::iterator iter = vec.begin(); iter != vec.end(); iter++ )
	{
		CString str;
		str.Format("ID:%d, x:%d, y:%d, ",(*iter).ID,(*iter).x,(*iter).y);
		MessageBox(str,_T("提示"),MB_ICONINFORMATION);
		
		int nIndex =0;
		for (vector<int>::iterator iterB = (*iter).B.begin(); iterB != (*iter).B.end(); iterB++ )
		{
			CString str;
			str.Format("B[%d]元素:%d ",nIndex++,*iterB);
			MessageBox(str,_T("提示"),MB_ICONINFORMATION);
		}
		
	}
	
	
}
谢谢。你的方法可以。就是还是不能很好的在unicode字符集下面使用。在网上查了一下,用了这个方法解决类型的转换:

char szistr[MAX_CHAR]={0};
wcstombs(szistr, str, str.GetLength());
const char* p = szistr;
istrstream str_roi(p);
int nvalue, n=0;
while (str_roi>>nvalue)
{
       ***********
}
留下来,方便其他人参考。
kuankuan_qiao 2014-04-14
  • 打赏
  • 举报
回复
http://www.jizhuomi.com/software/234.html 看看后面的 读写结构体
kuankuan_qiao 2014-04-14
  • 打赏
  • 举报
回复
引用 11 楼 xiaosongchao 的回复:
[quote=引用 9 楼 WUYUAN2011WOAINI 的回复:] [quote=引用 6 楼 xiaosongchao 的回复:] [quote=引用 4 楼 WUYUAN2011WOAINI 的回复:]
用CFile::Read 直接读取结构体就行了  
virtual UINT Read( void* lpBuf, UINT nCount );
lpBuf

Pointer to the user-supplied buffer that is to receive the data read from the file.
  
[/quote 没有很明白你的意思
你不是要读结构体嘛 用MFC中的CFile 直接放读到结果体中不就行了 [/quote] 可能我表达错了!是读txt文件里面的数据,给结构体中的对应项赋值。不是读结构体[/quote] 我说的就是读数据到结构体中 直接读就行了 干嘛一行一行多麻烦 一句话的事 干嘛扯这么多函数 你上网搜索 怎么读文件到结构体中
「已注销」 2014-04-14
  • 打赏
  • 举报
回复
思路:用到cstdiofile来读一行到cstring对象中,然后cstring转char* ,用字符串流来从字符中分离整数 下面代码测试通过,楼主应该会用,需包含头文件: #include <vector> #include <strstream> using namespace std;
// 结构体
struct data {
	int ID;
	int x;
	int y;
	vector<int> B;
};

// 获得信息到vec中
void GetData(vector<data>& vec,char* szFilePath){
	CStdioFile file;
	file.Open(szFilePath,CFile::modeReadWrite);// data.txt路径
	CString strLine;
	
	while(file.ReadString(strLine)) // 得到每一行数据
	{
		data T_data;
		int nValue,n=0;

		// LPCTSTR,多字节它为char* ,unicode它是wchar,此处参数为char*
		istrstream istr((LPCTSTR)strLine);
		while(istr>>nValue){
				n++;
			switch(n){
			case 1:
					T_data.ID = nValue;break;
			case 2:
					T_data.x = nValue;break;
			case 3:
					T_data.y = nValue;break;
			default:
				T_data.B.push_back(nValue);
				break;
			}
		}
		vec.push_back(T_data);
	}
}

// 测试显示数据是否正确
void CMfc001Dlg::OnOK() 
{
	// TODO: Add extra validation here
	vector<data> vec;
	GetData(vec,"I:\\data.txt");
	
	for (vector<data>::iterator iter = vec.begin(); iter != vec.end(); iter++ )
	{
		CString str;
		str.Format("ID:%d, x:%d, y:%d, ",(*iter).ID,(*iter).x,(*iter).y);
		MessageBox(str,_T("提示"),MB_ICONINFORMATION);
		
		int nIndex =0;
		for (vector<int>::iterator iterB = (*iter).B.begin(); iterB != (*iter).B.end(); iterB++ )
		{
			CString str;
			str.Format("B[%d]元素:%d ",nIndex++,*iterB);
			MessageBox(str,_T("提示"),MB_ICONINFORMATION);
		}
		
	}
	
	
}
mujiok2003 2014-04-14
  • 打赏
  • 举报
回复
引用 17 楼 xiaosongchao 的回复:
[quote=引用 16 楼 mujiok2003 的回复:] [quote=引用 15 楼 xiaosongchao 的回复:] 不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
std::wstring ---> openmode, 要干什么?[/quote]

m_file.ReadString(str);
	cout<<"Line Readed--> "<<str<<endl;
	wstring  str_s, cc;
	str_s = str.GetBuffer(0);
	istringstream str_roi(str_s);
	for (int i=0; i<(Bmax+3); i++)
	{
		str_roi>>cc;
		cout<<cc<<endl;
	}
	str.ReleaseBuffer();
其中,istringstream str_roi(str_s);这一句通过不了编译。。。怎么尽出一些我不熟悉的问题呢。。。。[/quote] 使用iwstringstream
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
引用 16 楼 mujiok2003 的回复:
[quote=引用 15 楼 xiaosongchao 的回复:] 不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
std::wstring ---> openmode, 要干什么?[/quote]

m_file.ReadString(str);
	cout<<"Line Readed--> "<<str<<endl;
	wstring  str_s, cc;
	str_s = str.GetBuffer(0);
	istringstream str_roi(str_s);
	for (int i=0; i<(Bmax+3); i++)
	{
		str_roi>>cc;
		cout<<cc<<endl;
	}
	str.ReleaseBuffer();
其中,istringstream str_roi(str_s);这一句通过不了编译。。。怎么尽出一些我不熟悉的问题呢。。。。
mujiok2003 2014-04-14
  • 打赏
  • 举报
回复
引用 15 楼 xiaosongchao 的回复:
不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
std::wstring ---> openmode, 要干什么?
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
引用 13 楼 mujiok2003 的回复:
[quote=引用 8 楼 xiaosongchao 的回复:] [quote=引用 7 楼 mujiok2003 的回复:] [quote=引用 5 楼 xiaosongchao 的回复:] 有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;
cstring str;??? [/quote] 错了,是string str;[/quote] 估计你的字符集是unicode的, 试试std::wstring[/quote]

不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
引用 13 楼 mujiok2003 的回复:
[quote=引用 8 楼 xiaosongchao 的回复:] [quote=引用 7 楼 mujiok2003 的回复:] [quote=引用 5 楼 xiaosongchao 的回复:] 有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;
cstring str;??? [/quote] 错了,是string str;[/quote] 估计你的字符集是unicode的, 试试std::wstring[/quote] 是的!工程一开始就用了unicode,不想改了。要改太多了。
mujiok2003 2014-04-14
  • 打赏
  • 举报
回复
引用 8 楼 xiaosongchao 的回复:
[quote=引用 7 楼 mujiok2003 的回复:] [quote=引用 5 楼 xiaosongchao 的回复:] 有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;
cstring str;??? [/quote] 错了,是string str;[/quote] 估计你的字符集是unicode的, 试试std::wstring
kuankuan_qiao 2014-04-14
  • 打赏
  • 举报
回复
引用 5 楼 xiaosongchao 的回复:
[quote=引用 3 楼 mujiok2003 的回复:] [quote=引用 2 楼 xiaosongchao 的回复:] [quote=引用 1 楼 mujiok2003 的回复:] 从文件流中读入一行(std::getline),再用这行构建一个字符串流(std::istringstream),再使用这个字符串流解析各个字段,很简单的(5~6代码的样子)
你这个是不是需要使用输入输出流,ifstream 可不可以只使用CSdioFile完成?[/quote] 用C FILE*或C++ stream是可以的, 用MFC的文件流应该也没有问题。 [/quote] 有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;[/quote] 什么错 cstring 注意大小写
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
引用 9 楼 WUYUAN2011WOAINI 的回复:
[quote=引用 6 楼 xiaosongchao 的回复:] [quote=引用 4 楼 WUYUAN2011WOAINI 的回复:]
用CFile::Read 直接读取结构体就行了  
virtual UINT Read( void* lpBuf, UINT nCount );
lpBuf

Pointer to the user-supplied buffer that is to receive the data read from the file.
  
[/quote 没有很明白你的意思
你不是要读结构体嘛 用MFC中的CFile 直接放读到结果体中不就行了 [/quote] 可能我表达错了!是读txt文件里面的数据,给结构体中的对应项赋值。不是读结构体
kuankuan_qiao 2014-04-14
  • 打赏
  • 举报
回复
引用 9 楼 WUYUAN2011WOAINI 的回复:
[quote=引用 6 楼 xiaosongchao 的回复:] [quote=引用 4 楼 WUYUAN2011WOAINI 的回复:]
用CFile::Read 直接读取结构体就行了  
virtual UINT Read( void* lpBuf, UINT nCount );
lpBuf

Pointer to the user-supplied buffer that is to receive the data read from the file.
  
[/quote 没有很明白你的意思
你不是要读结构体嘛 用MFC中的CFile 直接放读到结果体中不就行了 [/quote] 不好意思 是结构体
kuankuan_qiao 2014-04-14
  • 打赏
  • 举报
回复
引用 6 楼 xiaosongchao 的回复:
[quote=引用 4 楼 WUYUAN2011WOAINI 的回复:]
用CFile::Read 直接读取结构体就行了  
virtual UINT Read( void* lpBuf, UINT nCount );
lpBuf

Pointer to the user-supplied buffer that is to receive the data read from the file.
  
[/quote 没有很明白你的意思
你不是要读结构体嘛 用MFC中的CFile 直接放读到结果体中不就行了
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
引用 7 楼 mujiok2003 的回复:
[quote=引用 5 楼 xiaosongchao 的回复:] 有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;
cstring str;??? [/quote] 错了,是string str;
mujiok2003 2014-04-14
  • 打赏
  • 举报
回复
引用 5 楼 xiaosongchao 的回复:
有一个问题:我在MFC环境下运行这一句:str = cstr.getbuffer(0);不能通过编译。 CString cstr; cstring str;
cstring str;???
小宋朝 2014-04-14
  • 打赏
  • 举报
回复
[quote=引用 4 楼 WUYUAN2011WOAINI 的回复:]
用CFile::Read 直接读取结构体就行了  
virtual UINT Read( void* lpBuf, UINT nCount );
lpBuf

Pointer to the user-supplied buffer that is to receive the data read from the file.
  
[/quote 没有很明白你的意思
加载更多回复(5)

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧