当前位置: 首页 > news >正文

文件操作(个人学习笔记黑马学习)

C++中对文件操作需要包含头文件<fstream >


文件类型分为两种:
1.文本文件:文件以文本的ASCII码形式存储在计算机中

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

操作文件的三大类:
1.ofstream: 写操作
2. ifstream:读操作
3. fstream :读写操作

文件打开方式:

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

文本文件

1、写文件

#include <iostream>
using namespace std;
#include <string>
#include <fstream>//文本文件 写文件
void test01() {//1、包含头文件 fstream//2、创建流对象ofstream ofs;//指定打开方式ofs.open("Test.txt", ios::out);//4、写内容ofs << "姓名:张三" << endl;ofs << "性别:男" << endl;ofs << "年龄:18" << endl;//5、关闭文件ofs.close();
}int main() {test01();system("pause");return 0; 
}


2、读文件

#include <iostream>
using namespace std;
#include <string>
#include <fstream>//读文件
void test01() {//1、包含头文件//2、创建流对象ifstream ifs;//3、打开文件 并且判断是否打开成功ifs.open("test.txt", ios::in);if (!ifs.is_open()) {cout << "文件打开失败" << endl;return;}//4、读数据//第一种/*char buf[1024] = { 0 };while (ifs >> buf) {cout << buf << endl;}*///第二种/*char buf[1024] = { 0 };while (ifs.getline(buf,sizeof(buf))){cout << buf << endl;}*///3、第三种/*string buf;while (getline(ifs, buf)) {cout << buf << endl;}*///4、第四种char c;while ((c = ifs.get()) != EOF) { //EOF end of filecout << c;}//5、关闭文件ifs.close();
}int main() {test01();system("pause");return 0; 
}

二进制文件

1、写文件

#include <iostream>
using namespace std;
#include <string>
#include <fstream>class Person {
public:char m_Name[64];int m_Age;
};void test01() {//1、包含头文件//2、创建流对象ofstream ofs("person.txt", ios::out | ios::binary);//3、打开文件//ofs.open("person.txt", ios::out | ios::binary);//4、写文件Person p = { "张三",18 };ofs.write((const char*)&p, sizeof(Person));//5、关闭文件ofs.close();
}int main() {test01();system("pause");return 0; 
}

2、读文件

#include <iostream>
using namespace std;
#include <string>
#include <fstream>class Person {
public:char m_Name[64];int m_Age;
};void test01() {//1、包含头文件//2、创建流对象ifstream ifs;//3、打开文件 判断文件是否打开成功ifs.open("person.txt", ios::in | ios::binary);if (!ifs.is_open()) {cout << "文件打开失败" << endl;return;}//4、读文件Person p;ifs.read((char*)&p, sizeof(Person));cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;//5、关闭文件ifs.close();
}int main() {test01();system("pause");return 0; 
}

http://www.lryc.cn/news/164379.html

相关文章:

  • Android Studio新版本New UI及相关设置丨遥遥领先版
  • 新型人工智能技术让机器人的识别能力大幅提升
  • 聚观早报|蚂蚁集团发布“蚁天鉴”;vivo X100系列即将亮相
  • 读高性能MySQL(第4版)笔记05_优化服务器设置
  • Spring Boot跨域问题简介
  • 【Java】过滤器和拦截器区别
  • es滚动查询分析和使用步骤
  • 飞书公式总结
  • vue3.2 导出pdf文件或表格数据
  • Linux学习--MySQL学习之查询语句
  • 三、视频设备的枚举以及插拔检测
  • Qt开发_调用OpenCV(4.x)完成人脸检测并绘制马赛克(摄像头实时数据)
  • ssl证书有效期为什么越来越短?
  • XFF漏洞利用([SWPUCTF 2021 新赛]Do_you_know_http)
  • Java——》JVM对原生的锁做了哪些优化
  • 华为云云耀云服务器L实例评测|用docker搭建frp服务测试
  • 群狼调研(长沙满意度调查专业公司)开展公交车乘客满意度调查
  • Spring与OAuth2:实现第三方认证和授权的最佳实践
  • Mysql的定时备份与恢复
  • 学习Java基础面试题第五天
  • (10)(10.9) 术语表(一)
  • 面试(类加载器)
  • 二维差分---基础算法
  • C++之结构体智能指针shared_ptr实例(一百九十四)
  • 初出茅庐的小李博客之根据编译时间生成软件版本号
  • “投资教父”熊晓鸽老了,IDG光环不再
  • XEX智能交易所:加密货币衍生品杠杆、期货和期权简介
  • 记录第一次带后端团队
  • Python文件操作(02):读文件
  • Flink(java版)