C++读取文件的方式有哪些
这篇文章主要讲解了“C++读取文件的方式有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++读取文件的方式有哪些”吧!
C++可以根据不同的目的来选取文件的读取方式,目前为止学习了C++中的四种文件读取方式。
C++文件读取的一般步骤:
1、包含头文件 #include<fstream>
2、创建流对象:ifstream ifs(这里的ifs是自己起的流对象名字)
3、打开文件:file.open("文件路径","打开方式"),打开文件后并判断文件是否打开成功,ifs.is_open()是用于判断文件是否打开的语句
4、进行文件读取操作
5、关闭文件
ifs.close();
第一种方法:采用“<<”运算符
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
// 创建流对象
ifstream ifs;
//打开文件并判断是否打开成功如果成功则读取数据
readFile.open("test.txt", ios::in);
if (ifs.is_open())
{
cout << "文件打开成功" << endl;
//第一种方法
char buf[100] = { 0 };
while (ifs >>buf)
{
cout << buf << endl;
}
}
else
{
cout << "文件打开失败" << endl;
}
//关闭文件
ifs.close();
return 0;
}
或者可以写成以下,将判断是否成功打开文件换一种方式实现读取数据
#include<iostream>
#include<fstream>
using namespace std;
void test01()
{
// 2创建流文件
ifstream ifs;
// 3打开文件并判断是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "打开失败" << endl;
}
//读数据第一种方法
char buff[1024] = {0};
while (ifs >> buf)
{
cout << buf << endl;
}
解释:这种方式和采用cin>>进行写入类型,ifs>>buf进行写入时永远是从buff[0]开始,不会进行追加式的写入,ifs在遇到EOF(End Of File)时会返回False,以此来跳出while循环。
局限性:由于“<<”运算符自身的性质,会将一切空格当作终止条件,这样的话就无法输出空格信息,采用这种方法会将一行信息进行分段读出,对于类对象的信息读取是很有用的
第二种方法:利用<string>中的getline函数,按行读取,这样每一行内的所有空格也可以顺利读取
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("test.txt", ios::in);
if (readFile.is_open())
{
cout << "文件打开成功" << endl;
string buf;
while (getline(ifs,buf))
{
cout << buf << endl;
}
}
else
{
cout << "文件打开失败" << endl;
}
//关闭文件
ifs.close();
return 0;
}
同样,也有两种方式
#include<iostream>
#include<fstream>
using namespace std;
void test01()
{
// 2创建流文件
ifstream ifs;
// 3打开文件并判断是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "打开失败" << endl;
}
//第二种
string buf;
while(getline(ifs,buf))
{
cout << buf << endl;
}
//5 关闭文件
ifs.close();
}
getline()函数会在文件读完时返回False跳出while循环,getline可以一次读取一行文件,保持读出内容和文件内容一致。
第三种方法: 利用ifs流对象内部的getline方法,这个名字虽然和第二种中的一样,但是传入的参数不同,并不是同一个函数
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
//创建流对象
ifstream ifs;
readFile.open("test.txt", ios::in);
//判断文件是否成功打开成功则读取数据
if (ifs.is_open())
{
cout << "文件打开成功" << endl;
char buf[100] = { 0 };
while (ifs.getline(buf,sizeof(buf)))
{
cout << buf << endl;
}
}
else
{
cout << "文件打开失败" << endl;
}
//关闭文件
ifs.close();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
void test01()
{
// 2创建流文件
ifstream ifs;
// 3打开文件并判断是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "打开失败" << endl;
}
//读取数据的第三种方法
char buf[100] = { 0 };
while (ifs.getline(buf, sizeof(buf)))
{
cout << buf << endl;
}
//5 关闭文件
ifs.close();
}
第四种方法:利用ifs内部的get()函数一个个的将字符读出来,遇到EOF时返回False退出while循环,EOF 是 end of file的标志,不太建议使用这个,因为这个很慢,只能一个一个字符读取
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//创建流对象
ifstream ifs;
ifs.open("test.txt", ios::in);
//判断是否成功打开如果打开则读取数据
if (ifs.is_open())
{
cout << "文件打开成功" << endl;
char buf = 0;
while ((buf=ifs.get())!=EOF)
{
cout << buf;
}
}
else
{
cout << "文件打开失败" << endl;
}
//关闭文件
ifs.close();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
void test01()
{
// 2创建流文件
ifstream ifs;
// 3打开文件并判断是否打开成功
ifs.open("text.txt", ios::in);
if (!ifs.is_open())
{
cout << "打开失败" << endl;
}
// 第四种读取方法
char c;
while(((c = ifs.get()) != EOF))
{
cout << c;
}
//5 关闭文件
ifs.close();
}
相关内容
这些是最新的
热门排行
- THINKPHP5+GatewayWorker+Workerman 开发在线客服系统
- 在手机浏览器网页中点击链接跳转到微信界面的方法
- 尊云网站目录系统 ThinkPHP5网站分类目录程序 v2.2.221011
- CentOS 7安装shadowsock(一键安装脚本)
- AdminTemplate 基于LayUI 2.4.5实现的网站后台管理模板
- 用NW.js(node-webkit)开发多平台的桌面客户端
- PHP生成随机昵称/用户名
- THINKPHP5网站分类目录程序 尊云网站目录系统
- 织梦(DEDECMS)微信支付接口 微信插件
- 基于LayUI开发的 网站后台管理模板 BeginnerAdmin
- 响应式后台网站模板 - AMA.ADMIN
- layuiAdmin后台管理模板 Iframe版
- LayUI 1.0.9 升级 至 LayUI 2.1.4 方法
- 简洁清爽的会员中心模板
- jQuery幸运大转盘抽奖活动代码