#include <fstream>
#include <cassert>
#include <string>
逐行读出文件数据
void readTxt(string file)
{
ifstream infile;
infile.open(file.data());
assert(infile.is_open());
string s;
while(getline(infile,s))
{
cout<<s<,endl;
}
infile.close();
}
逐个字符读入(忽略空格与回车**
```cpp
void readTxt(string file)
{
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
char c;
while (!infile.eof())
{
infile >> c;
cout<<c<<endl;
}
infile.close(); //关闭文件输入流
}
```逐个字符读入(包括空格与回车)
```cpp
void readTxt(string file)
{
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
char c;
infile >> noskipws;
while (!infile.eof())
{
infile>>c;
cout<<c<<endl;
}
infile.close(); //关闭文件输入流
}