65,209
社区成员
发帖
与我相关
我的任务
分享
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字符集。多字节下可以直接使用。
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)
{
***********
}
留下来,方便其他人参考。// 结构体
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);
}
}
}
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
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);这一句通过不了编译。。。怎么尽出一些我不熟悉的问题呢。。。。
不能将参数 1 从“std::wstring”转换为“std::ios_base::openmode
用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
没有很明白你的意思