#include<iostream>
#include<string>
#include<fstream> //1、包含头文件
#define MaxSize 1024
using namespace std;
int main()
{
ifstream ifs; //2、创建流对象
ifs.open("mf.txt", ios::in); //3、打开文件 路径+文件名 打开方式
if (!ifs.is_open()) // 判断文件是否成功打开
{
cout << "文件打开失败" << endl;
return 0;
}
//4、写文件的内容
#if 0
{//方法一
char buf[1024] = { 0 };
while (ifs >> buf)
{
cout << buf << endl;
}
}
{//方法二
char buf[1024] = { 0 };
while (ifs.getline(buf,sizeof(buf)))
{
cout << buf << endl;
}
}
{//方法三
string buf;
while (getline(ifs,buf))
{
cout << buf << endl;
}
}
#elif 1
{//方法四
char c;
while ((c = ifs.get()) != EOF) //EOF 文件结束标志
{
cout << c;
}
}
#endif
ifs.close(); //5、关闭文件
system("pause");
return 0;
}
C++读文本文件
于 2024-01-27 13:07:08 首次发布