使用ifstream::get()方法从文本文件中读取数据

这是我的一个练习.与<写数据到文本文件的实现之一>配合使用.
写数据到文本文件的实现之一产生一个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; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值