读取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,