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;
}
相关推荐
shoubepatien7 分钟前
JAVA -- 05
java·开发语言
寰天柚子7 分钟前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
沐知全栈开发11 分钟前
Bootstrap 下拉菜单:设计与实现指南
开发语言
Queenie_Charlie24 分钟前
HASH表
数据结构·c++·哈希算法
Evand J36 分钟前
【MATLAB例程】多锚点RSSI定位和基站选择方法,基于GDOP、基站距离等因素。以Wi-Fi定位为例,附下载链接
开发语言·matlab·定位·gdop·rssi
superman超哥38 分钟前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉
JAVA+C语言1 小时前
String Constant Pool
java·开发语言
郝学胜-神的一滴1 小时前
OpenGL的glDrawElements函数详解
开发语言·c++·程序人生·游戏·图形渲染
WBluuue1 小时前
AtCoder Beginner Contest 436(ABCDEF)
c++·算法
moxiaoran57531 小时前
Go语言结构体
开发语言·后端·golang