您的位置:首页 > 其它

读取txt文档中的数值,有一定的协议,读取后求和,并计算出平均值

2013-05-28 19:37 399 查看
// TODO: Add your control notification handler code here
CFile file("11.txt",CFile::modeRead);//创建文件,关联路径和读取方式
char *pBuf; //定义字符指针,接收文档内容,
int len=file.GetLength();//获取文件内容长度
pBuf=new char[len+1];//指针空间
file.Read(pBuf,len);//读取内容
pBuf[len]='\0';//字符串末字符
file.Close();//关闭文件
//	MessageBox(pBuf);

int sum=0;//求和
int count=0;//数据个数
for (int i=0;i<len;i++)
{
if (pBuf[i]=='A'&&pBuf[i+1]=='+')//协议格式
{
count++;
CString tmp;
tmp+=pBuf[i+2];
tmp+=pBuf[i+3];
tmp+=pBuf[i+4];
tmp+=pBuf[i+5];
tmp+=pBuf[i+6];
tmp+=pBuf[i+7];读取格式后6为字符

int inttmp=atoi(tmp);字符串转化为整型
sum+=inttmp;//求和

}
}

CString s;
s.Format("%d",sum/count);//求平均值
GetDlgItem(IDC_EDIT1)->SetWindowText(s);//显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: