// 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);//显示
读取txt文档中的数值,有一定的协议,读取后求和,并计算出平均值
最新推荐文章于 2023-05-16 11:08:18 发布