C++入门新手村-文件读写知识讲解

文件读写知识讲解

C++简单文件操作

  • 文本文件 文件以文本的ASCII码形式存储在计算机中
  • 二进制文件 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

操作文件的三大类:

  • ofstream:写操作
  • ifstream:读操作
  • fstream:读写操作

读文件

读文件的基本步骤如下:

①包含头文件 #include <fstream>

②创建流对象 ifstream ifs;

③打开文件并判断文件是否打开成功 ifs.open("file uri",打开方式);

④读数据 四种方式读取

⑤关闭文件 ifs.close()

读文本文件

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream ifs;
	//打开文件
	ifs.open("C:/Users/darryl/Desktop/test.txt", ios::in);
	//判断文件是否打开
	if (ifs.is_open())
	{
		//读取文件四种方法
		//第一种读取方法
		/*char buf[1024] = { 0 };
		while (ifs >> buf)
		{
			std::cout << buf << std::endl;
		}*/
		//第二种读取方法
		/*char buf[1024] = { 0 };
		while (ifs.getline(buf,sizeof(buf)))
		{
			std::cout << buf << std::endl;
		}*/
		//第三种读取方法
		/*string buf;
		while (getline(ifs,buf))
		{
			std::cout << buf << std::endl;
		}*/
		//第四种读取方法
		char c;
		while ((c = ifs.get()) != EOF) //end of file 标记
		{
			std::cout << c;
		}
	}
	else
	{
		std::cout << "文件打开失败" << std::endl;
		return 0;
	}
	//关闭文件
	ifs.close();
	return 0;
}

读二进制文件

二进制读文件主要利用流对象调用成员函数 read

函数原型:istream& read(char* buffer , int len);

参数解读:字符指针buffer指向内存中的一段存储空间,len是读取的字节数

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person
{
private:
	//姓名
	string m_Name;
	//年龄
	int m_Age;
public:
	string getName()
	{
		return this->m_Name;
	}

	int getAge()
	{
		return this->m_Age;
	}
};

int main()
{
	ifstream ifs;
	ifs.open("C:/Users/darryl/Desktop/test,txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		std::cout << "文件打开失败!" << std::endl;
		return 0;
	}
	Person person;
	ifs.read((char*)&person, sizeof(Person));
	std::cout << "姓名:" << person.getName() << " 年龄:" << person.getAge() << std::endl;
	ifs.close();
}

写文件

写文件的基本步骤如下:

①包含头文件 #include <fstream>

②创建流对象 ofstream ofs;

③打开文件 ofs.open("file uri");

④写数据 ofs << "写入的数据";

⑤关闭文件 ofs.close();

文件打开方式:

  • ios::in 为读文件而打开文件
  • ios::out 为写文件而打开文件
  • ios::ate 初始位置:文件尾
  • ios::app 追加方式写文件
  • ios::trunc 如果文件存在先删除再创建
  • ios::binary 二进制方式

打开文件方式可以配合使用,利用 | 操作符:ios::binary | ios::out

写文本文件

cpp 复制代码
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//创建流对象
	ofstream ofs;
	//指定打开访问
	ofs.open("C:/Users/25763/Desktop/test.txt", ios::out);
	//写内容
	ofs << "姓名 : 张三" << std::endl;
	//关闭文件
	ofs.close();
}

写二进制文件

以二进制的方式对文件进行读写操作,打开方式需要指定为:ios::binary

二进制方式写文件主要利用流对象调用成员函数 write

函数原型:ostream& write(const char* buffer , int len);

参数解读:字符指针buffer指向内存中的一段存储空间,len是读写的字节数

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person
{
private:
	//姓名
	string m_Name;
	//年龄
	int m_Age;
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
};

int main()
{
	ofstream ofs;
	ofs.open("C:/Users/darryl/Desktop/test,txt", ios::out | ios::binary);
	Person person = { "张三",12 };
	ofs.write((const char*)&person, sizeof(Person));
	ofs.close();
}
相关推荐
倒头就睡的小比特15 小时前
算法竞赛C++常用的STL
c++·算法
weilx123415 小时前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!15 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读16 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
Smileyqp沛沛16 小时前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车17 小时前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光17 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·17 小时前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁17 小时前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven18 小时前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法