C++IO流

C++的输入与输出简称IO流。

简单概述:

在这里插入图片描述
通过输出流向目标设备写入数据输入流就是从设备读入数据
设备分为三种:1. 文件 2.控制台 3.特定的数据类型(stringstream)。

C++处理输入与输出(IO)需要借助特定的已经定义好的类,如下图是与IO流相关类信息继承结构图,C++的IO类库:

在这里插入图片描述

文件流:

对文件进行读写操作就称为文件流
需要添加头文件 <fstream>
ifstream : 对文件输入 (读文件)
ofstream : 对文件输出 (写文件)
fstream : 对文件输入或输出

对文件读写时,会有几种打开方式如下:

在以下打开方式中, 可以使用位操作 | 组合起来一起使用
在这里插入图片描述

文本文件的读写:

写入操作,代码如下:

#include <iostream>
#include <fstream>
#include <string>

#define FILE_ADDRESS "text.txt"	//要操作(读或写)的文件

using namespace std;

int main(void) {

	ofstream ofile;
	string name;
	int age;

	// 以写方式和尾部追加方式打开文件 FILE_ADDRESS
	// 若文件不存在,则自动创建一个文件(创建文件名与该名相同)
	ofile.open(FILE_ADDRESS, ios::out | ios::app);

	while (true)
	{
		cout << "[ctrl + z结束]请输入姓名:";
		cin >> name;

		// 判断打开文件是否结束
		if (cin.eof()) break;

		cout << "请输入年龄:";
		cin >> age;

		ofile << name << "\t" << age << endl;
	}

	ofile.close();	//关闭文件流

	return 0;
}

执行代码结果:
在这里插入图片描述
我要写入的文件是事先还没创建的,当我执行完代码后系统会帮我创建一个文件:

在这里插入图片描述
读操作,代码如下:
就是读刚刚创建的文件其中的内容,将其打印到控制台。

#include <iostream>
#include <fstream>
#include <string>

#define FILE_ADDRESS "text.txt"	//要操作(读或写)的文件

using namespace std;

int main(void) {

	ifstream ifile;
	string name;
	int age;

	ifile.open(FILE_ADDRESS);

	while (true)
	{
		ifile >> name;
		if (ifile.eof()) break;	//判断文件是否结束

		ifile >> age;

		cout << "姓名: " << name << "\t性别: " << age << endl;
	}

	ifile.close();	//关闭文件流

	return 0;
}
二进制文件读写:

文本文件:写入数字1,实际上是写入(字符)‘1’。
二进制文件:写入数字1,实际上是写入整数1
写入‘T’,实际上是写入‘T’

二进制写文件,代码如下:

#include <iostream>
#include <fstream>
#include <string>

#define FILE_ADDRESS "www.dat"

using namespace std;

int main(void)
{
	ofstream ofile;
	string name;
	int age;

	// 以二进制写入的方式打开文件
	ofile.open(FILE_ADDRESS, ios::out | ios::binary | ios::trunc);

	if (ofile)
		cout << "文件打开成功!" << endl << endl;
	else
	{
		cout << "文件不存在!" << endl;
		return 0;
	}

	while (true)
	{
		cout << "[ctrl+z结束]请输入姓名:";
		cin >> name;

		if (cin.eof()) break;	//判断文件是否结束

		ofile << name << "\t";

		cout << "请输入年龄:";
		cin >> age;

		// ofile << name 这种写法是文本文件的写法
		// 二进制文件的写 要使用write()
		ofile.write((const char*)&age, sizeof(age));
	}

	ofile.close();

	return 0;
}

二进制读文件,代码如下:

#include <iostream>
#include <fstream>
#include <string>

#define FILE_ADDRESS "www.dat"

using namespace std;

int main(void)
{
	ifstream ifile;
	string name;
	int age;
	char c;

	// 以二进制写入的方式打开文件
	ifile.open(FILE_ADDRESS, ios::in | ios::binary);

	if (ifile)
		cout << "文件打开成功!" << endl << endl;
	else
	{
		cout << "文件不存在!" << endl;
		return 0;
	}

	while (true)
	{
		ifile >> name;
		if (ifile.eof()) break;

		ifile.read(&c, sizeof(c));	//过滤'\t'

		// 二进制读取要使用read()
		ifile.read((char*)&age, sizeof(age));

		cout << "姓名:" << name << "\t年龄:" << age << endl;
	}

	ifile.close();

	return 0;
}
C++IO流 实现复制文件:

代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define FILE_ADDRESS "朴树 - 送别.mp3"	//要操作(读或写)的文件
#define MAX_SIZE 1024
	
using namespace std;

int main(void) {

	ofstream ofile;	//写入流
	ifstream ifile;	//输入流
	char message[MAX_SIZE];

	// 以二进制方式打开,以二进制方式写入
	ifile.open(FILE_ADDRESS, ios::in | ios::binary);
	ofile.open("binary.mp3", ios::out | ios::binary | ios::trunc);

	if (ifile)
		cout << "文件打开成功!" << endl << endl;
	else
	{
		cout << "文件打开失败!" << endl;
		return -1;
	}

	while (true)
	{
		// 从ifile文件中读入MAX_SIZE个字节到message
		ifile.read(message, MAX_SIZE);
		if (ifile.eof()) break;
		
		// 写入
		ofile.write(message, MAX_SIZE);
	}
	
	ofile.close();	//关闭文件流
	ifile.close();

	return 0;
}
指定格式读写:

指定格式读:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define FILE_ADDRESS "www.txt"

using namespace std;

int main(void)
{
	ofstream ofile;
	string name;
	int age;

	stringstream s;	// 字符串流

	ofile.open(FILE_ADDRESS, ios::out | ios::trunc);

	if (ofile)
		cout << "文件打开成功!" << endl << endl;
	else
	{
		cout << "文件不存在!" << endl;
		return 0;
	}

	while (true)
	{
		cout << "[ctrl+z结束]请输入姓名:";
		cin >> name;
		if (cin.eof()) break;

		cout << "请输入年龄:";
		cin >> age;

		s << "姓名:" << name << "\t年龄:" << age << endl;
		ofile << s.str();
		s.str("");	//清空字符串流s
	}

	ofile.close();

	return 0;
}

指定格式写:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define FILE_ADDRESS "www.txt"

using namespace std;

int main(void)
{
	ifstream ifile;
	char name[32];
	int age;

	string line;

	ifile.open(FILE_ADDRESS, ios::in);

	if (ifile)
		cout << "文件打开成功!" << endl << endl;
	else
	{
		cout << "文件不存在!" << endl;
		return 0;
	}

	while (true)
	{
		// 从ifile里读取一行到line
		getline(ifile, line);
		if (ifile.eof()) break;
		
		//C语言用法
		sscanf_s(line.c_str(), "姓名:%s\t年龄:%d", name, sizeof(name), &age);
		cout << "姓名:" << name << "\t年龄:" << age << endl;
	}

	ifile.close();

	return 0;
}
随机读写-文件定位:

seekg

// 作用:设置输入流的位置
// 参数1:偏移量 参数2:(相对)起始位置
// 相对位置:beg开始位置 cur当前位置 end结束位置
seekg(off_type offset, ios::seekdir origin);

tellg

// 返回该输入流的当前位置(即距离文件的起始位置的偏移量)
tellg();

seekp

// 设置该输出流的位置
// 参数1:偏移量 参数2:(相对)起始位置
seekp(off_type _Off, ios_base::seekdir _Way);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值