C++学习笔记之对文件的操作2
转载自**
https://www.cnblogs.com/uniqueliu/archive/2011/08/03/2126680.html
**
什么都不说了,继续《C++学习笔记之对文件的操作<1>》的内容…
void open(const char *_Filename,ios_base::openmode _Mode = ios_base::in | ios_base::out,int _Prot = (int)ios_base::_Openprot
);
void open(const char *_Filename,ios_base::openmode _Mode
);
void open(const wchar_t *_Filename,ios_base::openmode _Mode = ios_base::in | ios_base::out,int _Prot = (int)ios_base::_Openprot
);
void open(const wchar_t *_Filename,ios_base::openmode _Mode
);
_Filename
The name of the file to open.
打开文件名_Mode
One of the enumerations in ios_base::openmode.
文件的打开方式(在ios_base::openmode中定义)_Prot
The default file opening protection.
默认进行文件打开时的保护
复制代码
OK,我们再来看看ios_base::openmode中定义的打开方式:
ios::in, to permit extraction from a stream.
ios::out, to permit insertion to a stream.
ios::app, to seek to the end of a stream before each insertion.
打开文件之后文件指针指向文件末尾,只能在文件末尾进行数据的写入
ios::ate, to seek to the end of a stream when its controlling object is first created.
打开文件之后文件指针指向文件末尾,但是可以在文件的任何地方进行数据的写入
ios::trunc, to delete contents of an existing file when its controlling object is created.
ios::binary, to read a file as a binary stream, rather than as a text stream.
① 文本形式输出到文件,我们完全可以在open函数的mode选项中调用
#include <iostream>
#include<fstream>
using namespace std;
const int num=20;
struct people
{char name[num];double weight;int tall;int age;char sex;
};
int main()
{people pe={"李勇",78.5,181,25,'f'};ofstream fout("people.txt",ios::out|ios::app);fout<<pe.name<<" "<<pe.age<<" "<<pe.sex<<" "<<pe.tall<<" "<<pe.weight<<" "<<"\n";fout.close();ifstream fin("people.txt");char ch[255];fin.getline(ch,255-1,0);cout<<ch;fin.close();return 0;
}
我们可以看到,people.txt文件中的内容和命令行中的一样。② 二进制形式输出到文件 为了能够让其用二进制方式输出文件,我们只需要把上面程序的第16行和17行换做
ofstream fout("people.txt",ios::binary);
fout.write((char*)&pe,sizeof pe);
#include <iostream>
#include<fstream>
using namespace std;
const int num=20;
struct people
{char name[num];double weight;int tall;int age;char sex;
};
int main()
{people pe={"李勇",78.5,181,25,'f'};ofstream fout("people.txt",ios::binary);fout.write((char*)&pe,sizeof pe);fout.close();people pe1={"张玲",65.4,165,62,'m'};ifstream fin("people.txt",ios::binary);fin.read((char*)&pe1,sizeof pe1);cout<<pe1.name<<" "<<pe1.age<<" "<<pe1.sex<<" "<<pe1.tall<<" "<< pe1.weight <<" "<<"\n";fin.close();return 0;
}
我们再来看看这个东东的输出,我们可以看到,以txt文档打开文件时候,会产生乱码。这就是因为txt文件是以文本方式打开的,所以我们看到的都是乱码。如下图:
呼呼,以上就是我自认的自己不是很懂的C++关于如何操作文件的记录,到这里了~~全文完 _
C++学习笔记之对文件的操作2相关推荐
- C++学习笔记之对文件的操作1
转载自** https://www.cnblogs.com/uniqueliu/archive/2011/08/03/2126545.html ** =前言= 我们在编写程序的时候,最密不可分的就是对 ...
- 安卓学习笔记28:文件流操作
文章目录 一.文件存储类型 (一)内部存储 1.内部文件概念 2.文件读写方法 3.缓存文件目录 (二)外部存储 1.外部文件概念 2.编程对SD卡数据读写 二.案例演示 - 文件操作 (一)运行结果 ...
- Serlvet学习笔记之四—对文件的操作
1.读文件 package com.demo; import java.io.BufferedReader; import java.io.FileReader; import java.io.Pri ...
- Linux_红帽8学习笔记分享_3(文件操作管理)
Linux_红帽8学习笔记分享_3(文件操作管理) 文章目录 Linux_红帽8学习笔记分享_3(文件操作管理) 1.Vi编辑器 1.1两种模式 1.2十种技巧 2.用户的家目录 2.1 su命令的使 ...
- MySQL学习笔记05【多表操作、三大范式、数据库的备份和还原】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- linux系统管理学习笔记之八---linux文件与目录的管理及权限
linux系统管理学习笔记之八---linux文件与目录的管理及权限 2010-01-05 09:00:49 标签:权限 管理 文件目录 linx [推送到技术圈] 版权声明:原创作品,允许转载,转载 ...
- Git 学习笔记 ignore 忽略文件
Git 学习笔记 ignore .gitignore 文件 添加目标到 gitignore IDEA 中初始化 Git 仓库 问题 1. 无法添加到 gitignore (手动添加也没效果) 2. 文 ...
- Python学习笔记D9(文件)
Python学习笔记D9(文件) 文件 1.打开文件 open(file, mode='r') 接收两个参数:文件名(file)和模式(mode),用于打开一个文件,并返回文件对象,如果该文件无法被打 ...
- Keras学习笔记---保存model文件和载入model文件
Keras学习笔记---保存model文件和载入model文件 保存keras的model文件和载入keras文件的方法有很多.现在分别列出,以便后面查询. keras中的模型主要包括model和we ...
最新文章
- node.js 验证路径有效性_怎样识别光伏谐波路径?试试这个算法
- 第十八课.动态图模型
- ubuntu14.04 部署atlas
- python自学免费课堂-推荐5个Python爬虫免费学习资源(B站高赞!)
- RocketMQ-控制台的安装与启动
- 通用即插即用(UPnP)
- MySQL 创建视图
- python语言控制结构是指_python程序的三种控制结构
- Linux内核之XArray
- Linux就这个范儿 第8章 我是Makefile
- java drawpolygon_Java如何绘制和填充有孔的Polygon
- TCP握手协议的巧计方法
- three.js 文本_使用Three-bmfont-text在Three.js中创建文本
- 合批/批量渲染 (Batch)、实例化Instancing
- Linux如何查看端口
- 《数学分析(上)》重要概念复习
- 实现 Excel 多列数据组合
- 鸿蒙 background_element设置渐变色
- 小包实用工具:国家代码大全
- Microsoft Visual SourceSafe的使用