C++(21):fstream的读取和写入

目录

[1 ios::out](#1 ios::out)

[2 ios::in和is_open](#2 ios::in和is_open)

[3 put()方法](#3 put()方法)

[4 get()方法](#4 get()方法)

[4.1 读取单个字符](#4.1 读取单个字符)

[4.2 读取多个字符](#4.2 读取多个字符)

[4.3 设置终结符](#4.3 设置终结符)

[5 getline()](#5 getline())


1 ios::out

打开文件用于写入数据。**如果文件不存在,则新建该文件;**如果文件原来就存在,则打开时清除原来的内容。

cpp 复制代码
#include <fstream>
#include <iostream>

using namespace std;    //引入整个命名空间

int main()
{
	fstream file;
	file.open("test.txt",ios::out);

	if (!file.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}

	return 0;
}

项目文件内不存在test.txt文件,则运行后生成。

2 ios::in和is_open

ios::in:打开文件用于读取数据。如果文件不存在,则打开出错

is_open :用于检查文件流是否成功打开并关联到文件。它返回一个布尔值,指示文件是否已打开。

如果将ios::out换成ios::in,则会提示文件打开失败。

3 put()方法

当 fstream 文件流对象调用 put () 方法时,该方法的功能就变成了向指定文件中写入单个字符。 put () 方法的语法格式如下: 其中,括号内用于指定要写入文件的字符。 该方法会返回一个调用该方法的对象的引用形式。

cpp 复制代码
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	fstream file;
	file.open("test.txt",ios::out);

	file.put('c');
	char c = 'w';
	file.put(c);
	file.close();

	return 0;
}

4 get()方法

fstream是C++标准库中用于文件输入输出的类,它继承自iostreamget()方法是fstream(以及其基类istream)提供的一个常用成员函数,主要用于从文件或输入流中读取字符

注意:

ios:in | ios::out:打开已存在的文件,既可读取其内容,也可向其写入数据。文件刚打开时,原有内容保持不变,如果文件不存在,则打开出错。

4.1 读取单个字符

cpp 复制代码
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	fstream file;
	file.open("test.txt",ios::in | ios::out);

    if (!file.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}

	char n;
	n = file.get();
	cout << n;
	n = file.get();
	cout << n;
	n = file.get();
	cout << n;
	file.close();

	return 0;
}

4.2 读取多个字符

首先在test.txt文件存入多行字符。

遇到分隔符时停止读取,但分隔符不会被提取。

cpp 复制代码
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	fstream file;
	file.open("test.txt",ios::in || ios::out);
	if (!file.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}

	char str[4][1024] = { 0 };
	//get方法读到换行符就停止了,所以每读完一行需要把该行的换行符读掉
	file.get(str[0],1024);//读取第1行
	file.get();
	file.get(str[1], 1024);//读取第2行
	file.get();
	file.get(str[2], 1024);//读取第3行
	file.get();
	file.get(str[3], 1024);//读取第4行

	for (int i = 0; i < 4; i++)
	{
		cout << str[i] << endl;
	}
	file.close();

	return 0;
}

4.3 设置终结符

get()支持人为设置终结符。

cpp 复制代码
	file.get(str[0], 1024, 't');//设置终结符为't',读到t停止
	cout << str[0];

5 getline()

类似get(),但会提取并丢弃分隔符。

cpp 复制代码
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	fstream file;
	file.open("test.txt",ios::in || ios::out);
	if (!file.is_open())
	{
		cout << "文件打开失败!" << endl;
		return 0;
	}

	char str[4][1024] = { 0 };
	for (int i = 0; i < 4; i++)
	{
		file.getline(str[i], 1024);//整行读取,排除终结符
	}


	for (int i = 0; i < 4; i++)
	{
		cout << str[i] << endl;
	}
	file.close();

	return 0;
}
相关推荐
fouryears_234172 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~3 小时前
C#---StopWatch类
开发语言·c#
lifallen4 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研4 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a5 小时前
组合模式及优化
c++·设计模式·组合模式
cui__OaO6 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
星星火柴9366 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
鱼鱼说测试6 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑7 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_027 小时前
【Java基础面试题】Java基础概念
java·开发语言