文本文件内容是1020-1-2,将10,20,-1,-2读四个字节到字符串后用StrToInt转换
char stemp1[5],stemp2[5];
int itemp[2];
myfile.Read(&stemp1,4);
CString str1=(CString)stemp1;
myfile.Read(&stemp2,4);
itemp[0]=StrToInt((CString)stemp1);
itemp[1]=StrToInt((CString)stemp2);
结果10变成了1,20变成了2。
char stemp1[5],stemp2[5];
int itemp[2];
myfile.Read(&stemp1,4);
CString str1=(CString)stemp1;
myfile.Read(&stemp2,4);
itemp[0]=StrToInt((CString)stemp1);
itemp[1]=StrToInt((CString)stemp2);
结果10变成了1,20变成了2。
char *p1=&str[5];
char *p2=&str[7];atoi(p1020);//=1020
atoi(p1);//=1
atoi(p2);//=2
// itemp[0] == 2010, stemp2应该是 -1 或者 -2
// 代码和lz描述的不符?哪里读出 20, 10, -1, -2 了?
{
CFile file(_T("F:\\11.txt"), CFile::modeRead);
DWORD dwSize = file.GetLength();
char* buf = new char[dwSize + 1];
memset(buf, 0, sizeof(char) * (dwSize + 1));
file.Read(buf, dwSize);
file.Close();
CString strText(buf);
for(int i=0; i<strText.GetLength(); i+=2)
{
CString strTmp = strText.Mid(i, 2);
int nVal = _ttoi(strTmp); //这里就是你需要的数据
}
delete[] buf;
buf = NULL;
}
catch (CException* e)
{
e->ReportError();
e->Delete();
}