stl 输入输出流

标准输入输出流

头文件 iostream

从标准输入读取流 cin >>

从标准输出写入流 cout <<

get 系列函数

get

无参数:cin.get() 从指定的输入流中提取一个字符(包括空白字符),若读取成功,返回该字符的 ASCII 值,若遇到文件结束符或读取错误,返回特殊值 EOF

有 1 个参数:cin.get(ch) 从输入流中读取一个字符赋给 ch

有 3 个参数:cin.get(字符数组,字符个数n,终止字符)

        cin.get(字符指针,字符个数n,终止字符) 从输入流中读取 n-1 个字符,赋给指定的字符数组/字符指针指向的数组,若在读取 n-1 个字符之前遇到指定的终止字符,则提前结束读取 遇到终止字符时,不读取终止字符,直接在字符串缓冲区尾部加结束标志 '\0'

getline

cin.getline(字符数组(或字符指针),字符个数n,终止标志字符) 从输入流中读取一行字符,用法与带 3 个参数的 get 函数类似 遇到终止字符时,读取终止字符,但不会将其存储到字符串缓冲区

流状态

  • goodbit:流未发生错误
  • eofbit:输入流遇到文件尾
  • failbit:流中发生格式错误,非致命错误,可挽回
  • badbit:发生数据丢失,致命错误,不可挽回

获取状态信息

int rdstate(); 无参数,返回值为状态信息特征值

检测输入输出状态

bool good(); 若返回 true,一切正常,没有错误发生

bool eof(); 若返回 true,表明已到达流的末尾

bool fail(); 若返回 true,表明 I/O 操作失败,主要原因是非法数据,但流可以继续使用

bool bad(); 发生了致命错误,流将不能继续使用

挽救方法

cin.clear(); 清除错误状态

cin.sync(); 清理错误字符

文件输入输出流

头文件:fstream

一个文件流对应一个物理文件

文件流只能按字符方式迭代

不同操作系统文件的编码模式不同

文件流有长度和索引两个属性

数据类型

数据类型

描述

ofstream

表示输出文件流,用于创建文件并向文件写入信息

ifstream

表示输入文件流,用于从文件读取信息

fstream

表示文件流,同时具有 ofstream 和 ifstream 两种功能,意味着其可以创建文件,向文件写入信息,从文件读取信息

打开文件

从文件读取信息或者向文件写入信息之前,必须先打开文件。ofstream 和 fstream 对象都可以用来打开文件进行写操作,如果只需要打开文件进行读操作,则使用 ifstream 对象

open

void open(const char *filename,ios::openmode mode);

第一个参数:指定要打开的文件的名称和位置

第二个参数:定义文件被打开的模式

模式标志

描述

ios::app

追加模式,所有写入都追加到文件末尾

ios::ate

文件打开后定位到文件末尾

ios::in

打开文件用于读取

ios::out

打开文件用于写入

ios::trunc

如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为0

PS:可以把以上两种或两种以上的模式结合使用,模式 A | 模式 B

is_open

is_open() 返回文件是否已经打开

关闭文件

当 C++ 程序终止时,会自动关闭刷新所有流,释放所有分配的内存,并关闭所有打开的文件

close

void close();

#include<iostream>
#include<fstream>
using namespace std;
int main() {
    char szBuf[80];
    ifstream in("a.txt");//打开文本文件
    if (!in) {//判断读取是否成功
        cout << "文件不存在" << endl;
        return 0;
    }
    while (in.getline(szBuf, 80)) {//读取并输出文本文件中的内容
        cout << szBuf << endl;
    }
    in.close();//关闭文本文件
    return 0;
}

写入文件

使用流插入运算符 << 向文件写入信息

write(const char* buff,streamsize_t buffSize) 写入 nSize 字节数据到文件中

ostream& write(const char*,int nSize) 写二进制文件

#include<iostream>
#include<fstream>
using namespace std;
typedef struct Student {
	char StuName[20];
	int StuGrade;
}Student;
int main() {
	ofstream out;
	out.open("student.txt");//打开文本文件
	Student stu1 = { "张三", 90 };
	Student stu2 = { "李四", 80 };
	out << stu1.StuName << "\t" << stu1.StuGrade << endl;//把成绩信息存到文本文件
	out << stu2.StuName << "\t" << stu2.StuGrade << endl;
	out.close();//关闭文本文件
	return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
typedef struct Student {
	char StuName[20];
	int StuGrade;
}Student;
int main() {
	ofstream out;
	out.open("student_2.txt");//打开文本文件
	Student stu1 = { "张三", 90 };
	Student stu2 = { "李四", 80 };
	out.write((const char*)&stu1, sizeof(Student));//把成绩信息存到二进制文件
	out.write((const char*)&stu2, sizeof(Student));
	out.close();//关闭文件
	return 0;
}

读取文件

使用流提取运算符 >> 从文件读取信息

read(const char* buff,streamsize_t buffSize) 读入最多 nSize 字节数据到 buff 中

istream& read(char*,int nSize) 读二进制文件

#include<iostream>
#include<fstream>
using namespace std;
typedef struct Student {
	char StuName[20];
	int StuGrade;
}Student;
int main() {
	ifstream in;
	in.open("student.txt");//打开文本文件
    if(!in)
        return 0;
	Student stu1;
	Student stu2;
	in >> stu1.StuName >>  stu1.StuGrade;//读文本文件信息
	in >> stu2.StuName >>  stu2.StuGrade;
	in.close();//关闭文本文件
	cout << "姓名:" << stu1.StuName << ' ' << "年龄:" << stu1.StuGrade << endl;//输出读取的信息
	cout << "姓名:" << stu2.StuName << ' ' << "年龄:" << stu2.StuGrade << endl;
	return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
typedef struct Student {
	char StuName[20];
	int StuGrade;
}Student;
int main() {
	ifstream in;
	in.open("student_2.txt");//打开文本文件
	if (!in)
		return 0;
	Student stu1;
	Student stu2;
	in.read((char*)&stu1, sizeof(Student));//从二进制文件中读取成绩信息
	in.read((char*)&stu2, sizeof(Student));
	in.close();//关闭文件
	cout << "姓名:" << stu1.StuName << ' ' << "年龄:" << stu1.StuGrade << endl;//输出读取的信息
	cout << "姓名:" << stu2.StuName << ' ' << "年龄:" << stu2.StuGrade << endl;
	return 0;
}

文件位置指针

流的位置标识

ios::beg:流的开始位置

ios::cur:流的当前位置

ios::end:流的结束位置

定位函数

seekg

istream& seekg(long relativepos,ios::seek_dir dir)

针对输入流

参数:第一个参数是要移动的字符数目,可正可负;第二个参数是移动方向,使用三种流的位置标识中的一个

含义:字符指针相对于移动方向向前或向后移动了多少个字符

out.seekg(n); 定位到 out 的第 n 个字节

out.seekg(n,ios::cur); 把文件的读指针从 out 当前位置后移 n 个字节

out.seekg(n,ios::end); 把文件的读指针从 out 末尾往回移动 n 个字节

out.seekg(0,ios::end); 定位到 out 的末尾

seekp

ostream& seekp(long relativepos,ios::seek_dir dir)

针对输出流

参数:第一个参数是要移动的字符数目,可正可负;第二个参数是移动方向,使用三种流的位置标识中的一个

含义:字符指针相对于移动方向向前或向后移动了多少个字符

tellg

针对输入流

含义:返回该输入流的当前位置(距离文件的起始位置的偏移量)

tellp

针对输出流

含义:返回该输出流的当前位置(距离文件的起始位置的偏移量)

输入输出流中的缓冲 streambuf

STL 提供了缓冲区类 streambuf,提供给输入输出流使用,每个标准的输入输出流对象均包含一个 streambuf 指针

rdbuf

可以调用 rdbuf() 获取 streambuf 指针,从而直接访问底层 streambuf 对象

调用方法:

  • 无参数:返回调用者的流缓冲指针
  • 参数为流缓冲指针:使调用者与参数关联,返回自己当前关联的流缓冲区指针

字符串输入输出流

字符串输入输出流类直接对内存而不是对文件和标准输出进行操作

头文件:<sstream>

数据类型

数据类型

描述

istringstream

字符串输入流,提供读 string 功能

ostringstream

字符串输出流,提供写 string 功能

stringstream

字符串输入输出流,提供读写 string 功能

str

void str() 无参数,用于将 stringstream 流中的数据以 string 字符串的形式输出

void str(const string& s) 以字符串为参数,用以覆盖 stringstream 流中的数据

ss.str(""); 字符串流清零,将流中的数据全部清除

clear

作用:清除流的错误状态,主要用在 stringstream 重复使用或者多个 stringstream 对象构造时清空,否则之前的缓冲就停留在输入输出流中

对同一个 stringstream 对象重复赋值,需要对流使用 clear() 函数清空流的状态,此时流占用的内存没有改变,会一直增加【stringstream 不主动释放内存】,若想改变内存【一般是清除内存,减少内存消耗】,需要配合使用 str("") 清空 stringstream 的缓存

读取 string 对象

描述:

  • istringstream 从 string 对象中读取
  • 可以从流中提取数据,支持 >> 操作
#include<iostream>
#include<sstream>
using namespace std;
int main() {
	int n;
	float f;
	string strHello;
	string strText = "1 3.14 hello";
	istringstream s(strText);//从string对象中读取
	s >> n;//从流中提取数据
	s >> f;
	s >> strHello;
	cout << "n=" << n << endl;
	cout << "f=" << f << endl;
	cout << "strHello=" << strHello << endl;
	return 0;
}

写入 string 对象

描述:

  • ostringstream 写到 string 对象中
  • 可以把其他类型的数据写入流(往流中写入数据),支持<<操作;
#include<iostream>
#include<sstream>
using namespace std;
int main() {
	cout << "type an int,a float and a string:";
	int i;
	float f;
	string stuff;
	cin >> i >> f;
	getline(cin, stuff);
	ostringstream os;
	os << "integer=" << i << endl;//把数据写到流中
	os << "float=" << f << endl;
	os << "string=" << stuff << endl;
	string result = os.str();//写到string对象中
	cout << result << endl;
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何hyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值