这是我的一个练习.与<写数据到文本文件的实现之一>配合使用.
写数据到文本文件的实现之一产生一个1.txt文件.这里要用到.
// 包含文件
#include <iostream>
#include <fstream>
#include <string>
// 名字空间的声明
using namespace std;
// 以下为按钮事件代码
ifstream getdata;
getdata.open("c://1.txt");
// 每次读到的字符
char chr;
char chr_space_or_other;
chr_space_or_other = 32; //数与数之间的分隔符号//
char chr_enter = 13; //回车//
char chr_array[10]={0}; //保存一个读到的数,字符串//
int int_save_data=0; //字符串转换成为一个数 //
int sum_all=0; //读到的数,求和//
int ii=0; //计数
while (!getdata.eof())
{
getdata.get(chr);
//如果读到换行或回车,什么也不做,继续读.
if ((chr==10) || (chr==13))
{
continue;
}
//如果不是分隔符
if (chr != chr_space_or_other)
{
chr_array[ii++]=chr;
chr_array[ii]='/0';
}
else //如果是分隔符
{
chr_array[ii++]='/0';
ii=0;
AfxMessageBox(chr_array); //显示读到的数据
int_save_data = atoi(chr_array); //转换为整数
sum_all = sum_all + int_save_data; //求和
CString Display_sum;
Display_sum.Format("The summary is %d. ",sum_all); //显示读到的数据之和
AfxMessageBox(Display_sum);
}
}
getdata.close();
return;