服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 带你粗略了解C++流的读写文件

带你粗略了解C++流的读写文件

2021-12-20 15:02_Space_ C/C++

这篇文章主要为大家总结了C++中输入输出流及文件流操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助

读写文本文件

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

设备有:
1)文件
2)控制台
3)特定的数据类型(stringstream)
C++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

带你粗略了解C++流的读写文件

C++的 IO类库为:

带你粗略了解C++流的读写文件

文件流:对文件进行读写操作
头文件: < 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的格式字符串中含有中文时,可能会读取失败。

 

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/qq_45140193/article/details/119789181

延伸 · 阅读

精彩推荐
  • C/C++c++ 单线程实现同时监听多个端口

    c++ 单线程实现同时监听多个端口

    这篇文章主要介绍了c++ 单线程实现同时监听多个端口的方法,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下...

    源之缘11542021-10-27
  • C/C++学习C++编程的必备软件

    学习C++编程的必备软件

    本文给大家分享的是作者在学习使用C++进行编程的时候所用到的一些常用的软件,这里推荐给大家...

    谢恩铭10102021-05-08
  • C/C++C/C++经典实例之模拟计算器示例代码

    C/C++经典实例之模拟计算器示例代码

    最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示...

    jia150610152021-06-07
  • C/C++C++之重载 重定义与重写用法详解

    C++之重载 重定义与重写用法详解

    这篇文章主要介绍了C++之重载 重定义与重写用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    青山的青6062022-01-04
  • C/C++C语言中炫酷的文件操作实例详解

    C语言中炫酷的文件操作实例详解

    内存中的数据都是暂时的,当程序结束时,它们都将丢失,为了永久性的保存大量的数据,C语言提供了对文件的操作,这篇文章主要给大家介绍了关于C语言中文件...

    针眼_6702022-01-24
  • C/C++深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网7342020-12-03
  • C/C++C语言实现电脑关机程序

    C语言实现电脑关机程序

    这篇文章主要为大家详细介绍了C语言实现电脑关机程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiaocaidayong8482021-08-20
  • C/C++详解c语言中的 strcpy和strncpy字符串函数使用

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数是字符串复制函数。接下来通过本文给大家介绍c语言中的strcpy和strncpy字符串函数使用,感兴趣的朋友跟随小编要求看看吧...

    spring-go5642021-07-02