IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++流(读写文件) -> 正文阅读

[C++知识库]C++流(读写文件)

读写文本文件

C++的IO流:
IO:向设备输入数据和输出数据

设备有:
1)文件
2)控制台
3)特定的数据类型(stringstream)

C++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

在这里插入图片描述

C++的 IO类库为:

在这里插入图片描述

文件流:对文件进行读写操作
头文件: < fstream >

ifstream 对文件输入(读文件)
ofstream 对文件输出(写文件)
fstream 对文件输入或输出

文件的打开方式:

模式标志描述
ios::in读方式打开文件
ios:out写方式打开文件
ios::trunc如果此文件已经存在, 就会打开文件之前把文件长度截断为0
ios::app尾部最加方式(在尾部写入)
ios::ate文件打开后, 定位到文件尾
ios::binary二进制方式(默认是文本方式)

写文本文件

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

using namespace std;

int main()
{
	string name;
	int age;
	ofstream outfile;  //也可以使用fstream, 但是fstream的默认打开方式不截断文件长度

	// ofstream的默认打开方式是,  截断式写入 ios::out |  ios::trunc
	// fstream的默认打开方式是,  截断式写入   ios::out
	// 建议指定打开方式
	outfile.open("user.txt", ios::out | ios::trunc);

	while (1) {
		cout << "[ctrl+z退出]" << endl;
		cout << "请输入姓名:";
		cin >> name;
		if (cin.eof()) { //判断文件是否结束
			break;
		}
		outfile << name << "\t";

		cout << "请输入年龄: ";
		cin >> age;
		outfile << age << endl;  //文本文件写入
	}

	// 关闭打开的文件
	outfile.close();

	system("pause");
	return 0;
}

写文本文件

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

using namespace std;

int main()
{
	string name;
	int age;
	ifstream infile;
	infile.open("user.txt");

	while (1) {
		infile >> name;
		if (infile.eof()) { //判断文件是否结束
			break;
		}
		cout << name << "\t";

		infile >> age;
		cout << age << endl;  
	}

	// 关闭打开的文件
	infile.close();

	system("pause");
	return 0;
}

二进制读写文件

写二进制文件
使用文件流对象的write方法写入二进制数据.

注:若 ***outfile << age << end;***
写入文件会转换到文本方式写入
需要使用write()吧整形转换到char类型,进行写入

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

using namespace std;

int main()
{
	string name;
	int age;
	ofstream outfile;
	outfile.open("user.dat", ios::out | ios::trunc | ios::binary);

	while (1) {
		cout << "请输入姓名: [ctrl+z退出] ";
		cin >> name;
		if (cin.eof()) { //判断文件是否结束
			break;
		}
		outfile << name << "\t";

		cout << "请输入年龄: ";
		cin >> age;  
		//outfile << age << endl;  //会自动转成文本方式写入
		outfile.write((char*)&age, sizeof(age));
	}

	// 关闭打开的文件
	outfile.close();

	system("pause");
	return 0;
}

二进制读文件

需使用read()吧写入的内容读取出来并输出

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

using namespace std;

int main()
{
	string name;
	int age;
	ifstream infile;
	infile.open("user.dat", ios::in | ios::binary);

	while (1) {
		infile >> name;
		if (infile.eof()) { //判断文件是否结束
			break;
		}
		cout << name << "\t";
	
		// 跳过中间的制表符
		char tmp;
		infile.read(&tmp, sizeof(tmp)); 

		//infile >> age; //从文本文件中读取整数, 使用这个方式
		infile.read((char*)&age, sizeof(age));
		cout << age << endl;  //文本文件写入
	}

	// 关闭打开的文件
	infile.close();

	system("pause");
	return 0;
}

按指定格式读写文件

指定格式写文件:
使用 < stringstream>

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

using namespace std;

int main()
{
	string name;
	int age;
	ofstream outfile;
	outfile.open("user.txt", ios::out | ios::trunc);

	while (1) {
		cout << "[ctrl+z退出]" << endl;
		cout << "请输入姓名: ";
		cin >> name;
		if (cin.eof()) { //判断文件是否结束
			break;
		}

		cout << "请输入年龄: ";
		cin >> age;
		
		stringstream s;
		s << "name:" << name << "\t\tage:" << age << endl;
		outfile << s.str();
	}

	// 关闭打开的文件
	outfile.close();

	system("pause");
	return 0;
}

指定格式读文件:
在C++指定格式读文件并没有优雅的解决方案
就用C语言的: sscanf

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>

using namespace std;

int main(void)
{
	char name[32];
	int age;
	string line;
	ifstream infile;
	infile.open("user.txt");

	while (1) {
		getline(infile, line);
		if (infile.eof()) { //判断文件是否结束
			break;
		}

		sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name, sizeof(name),&age);
		cout << "姓名:" << name << "\t\t年龄:" << age << endl;
	}

	infile.close();

	system("pause");
	return 0;
}

文件流的状态检查

描述
is_open()文件流是否打开成功
eof()流是否结束
fail()流的failbit或者badbit被置位时, 返回true
failbit: 出现非致命错误,可挽回, 一般是软件错误badbit:置位, 出现致命错误, 一般是硬件错误或系统底层错误, 不可挽回
bad()流的badbit置位时, 返回true
good()流处于有效状态时, 返回true
clear()流的所有状态都被复位
文件流的三种定位 seekg tellg seekp

seekg:

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

获取文件的最后50个字符:

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

using namespace std;

int main(void) {
	ifstream infile;

	infile.open(/*文件名字这里我就不写了*/".cpp");
	if (!infile.is_open()) {
		return 1;
	}
		//定位到最后50个字母
	infile.seekg(-50, infile.end);
	while (!infile.eof()) {
		string line;
		getline(infile, line);
		cout << line << endl;
	}

	infile.close();

	system("pause");
	return 0;
}

tellg:

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

获取文件的长度:

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

using namespace std;

int main(void) {
	ifstream infile;

	infile.open(/*文件名字这里我就不写了*/".cpp");
	if (!infile.is_open()) {
		return 1;
	}

	// 先把文件指针移动到文件尾
	infile.seekg(0, infile.end);

	int len = infile.tellg();
	cout << "len:" << len;

	infile.close();
	system("pause");
	return 0;
}

seekp

设置该输出流的位置

先向新文件写入:“123456789”
然后再在第4个字符位置写入"ABC"

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

using namespace std;

int main(void) {
	ofstream outfile;

	outfile.open("test.txt");
	if (!outfile.is_open()) {
		return 1;
	}

	outfile << "123456789";

	outfile.seekp(4, outfile.beg);
	outfile << "ABC";

	outfile.close();
	system("pause");
	return 0;
}
常见的错误

1.文件没有关闭, close(),可能导致写文件失败
2.文件打开方式不合适
3.在VS2015的部分版本中,当sscanf和sscanf_s的格式字符串中含有中文时,可能会读取失败。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 11:52:27  更:2021-08-19 11:52:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 15:50:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码