如何用vc读取固定格式的文本

text1 123 456
text2 读取 固定
text3 0000 1111

想在程序中有四个Edit编辑框为
Edit1 、Edit2 、Edit3 、Edit4 、Edit5 、Edit6
和一个按钮button1
想让程序识别 text1、text2、text3后面的数字或文本

点击按钮button1
然后程序就会在
在Edit1中显示 123
在Edit2中显示 456
在Edit3中显示 读取
在Edit4中显示 固定
在Edit5中显示 0000
在Edit6中显示 1111

下面是高手jia_xiaoxin 的解答:

  1. #include <fstream> 
  2. #include <string>
  3. using namespace std;
  4. void testdlg::GetFileText()
  5. {
  6.     string str;
  7.     ifstream infile("C://out.txt");  
  8.     
  9.     if(!infile.is_open())//文件打开出错检查 
  10.     { 
  11.         AfxMessageBox("open file error!"); 
  12.     } 
  13.     else
  14.     {
  15.         while(infile >> str) 
  16.         { 
  17.             if(str == "text1")
  18.             {
  19.                 infile >> str;
  20.                 SetDlgItemText(IDC_EDIT1, str.c_str());
  21.                 infile >> str;
  22.                 SetDlgItemText(IDC_EDIT2, str.c_str());
  23.             }
  24.             if(str == "text2")
  25.             {
  26.                 infile >> str;
  27.                 SetDlgItemText(IDC_EDIT3, str.c_str());
  28.                 infile >> str;
  29.                 SetDlgItemText(IDC_EDIT4, str.c_str());
  30.             }
  31.             if(str == "text3")
  32.             {
  33.                 infile >> str;
  34.                 SetDlgItemText(IDC_EDIT5, str.c_str());
  35.                 infile >> str;
  36.                 SetDlgItemText(IDC_EDIT6, str.c_str());
  37.             }
  38.         }
  39.     }
  40. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值