VS2013 C++/ Winform textBox 读txt文本 显示多行

  读取txt文件逐行:

   法一:

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>


   

	// 法一
				 set_xunfei_param();//参数设置
				 string file = "C:\\Users\\jingling\\Desktop\\1.txt";  //路径设置

				 ifstream infile;
				 infile.open(file.data());   //将文件流对象与文件连接起来 
				 assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行

				 string s;
				 while (getline(infile, s))
				 {
					 txt_speak->Text = gcnew String(s.c_str());
				 }
				 infile.close();             //关闭文件输入流 
/
法二:

/法二
				 /*char *filePath = "C:\\Users\\jingling\\Desktop\\1.txt";  //路径设置

				 ifstream file;
				 file.open(filePath, ios::in);
				 //assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行
				 
				std::string strLine;
				while (getline(file, strLine))
				{
					if (strLine.empty())
						continue;
					txt_speak->Text = gcnew String(strLine.c_str());
				}

				 file.close();             //关闭文件输入流 */


	     	///
若txt的文件内容是:

  423,

  265,

  456,

  123,

 法一和法二的结果是,textBox只显示一行:123,

	     	///
				 char *filePath = "C:\\Users\\jingling\\Desktop\\1.txt";  //路径设置

				 ifstream file;
				 file.open(filePath, ios::in);

				 std::string strLine;

				 while (getline(file, strLine))
				 {
				    if (strLine.empty())
				    continue;
					txt_speak->Text = txt_speak->Text + gcnew String(strLine.c_str());
				 }

				 file.close();             //关闭文件输入流 

把法一法二中的:

txt_speak->Text = gcnew String(strLine.c_str());

改为:

txt_speak->Text = txt_speak->Text + gcnew String(strLine.c_str());

textBox结果:

     423,256,456,123,























  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值