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

c++_ifstream,ofstream读写文件

ofstream是从内存到硬盘,ifstream是从硬盘到内存。

在c++中,有一个stream这个类,所有的I/O的操作都是以这个类为基础的。

stream这个类有两个重要的运算符:

1.插入器 <<

向流输出数据,例如cout << "write out";就是把字符串输出到标准输出流。

2.析取器 >>

从流中输入数据,例如cin >> x; 表示从标准输入流中读取一个指定类型的数据。

在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件<fstream.h>。

打开文件

在fstream类中,有一个成员函数open(),就是用来打开文件的,其原型是:

void open(const char* filename,int mode,int access);参数:filename:  要打开的文件名mode:    要打开文件的方式access:   打开文件的属性

打开文件的方式在类ios(是所有流式I/O类的基类)中定义,常用的值如下:

  ios::app:   以追加的方式打开文件ios::ate:   文件打开后定位到文件尾,ios:app就包含有此属性ios::binary: 以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文ios::in:    文件以输入方式打开(文件数据输入到内存)ios::out:   文件以输出方式打开(内存数据输出到文件)ios::nocreate: 不建立文件,所以文件不存在时打开失败ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败ios::trunc:  如果文件存在,把文件长度设为0

可以用“或”把以上属性连接起来,如ios::out|ios::binary

打开文件的属性取值是:

  0:普通文件,打开访问1:只读文件2:隐含文件4:系统文件

可以用“或”或者“+”把以上属性连接起来,如3或1|2就是以只读和隐含属性打开文件。

例如:以二进制输入方式打开文件 file.txt

  fstream file1;file1.open("file.txt",ios::binary|ios::in,0);

如果open函数只有文件名一个参数,则是以读/写普通文件打开,即:

file1.open("file.txt");
即为:
file1.open("file.txt",ios::in|ios::out,0);

fstream 还有和 open( )一样的构造函数,对于上例,在定义的时侯就可以打开文件了:

fstream file1("file.txt");

fstream 有两个子类: ifstream(input file stream) 和 ofstream(outpu file stream)

ifstream默认以输入方式打开文件

ofstream默认以输出方式打开文件。

  ifstream file1("aaa.txt");//以输入方式打开文件ofstream file2("bbb.txt");//以输出方式打开文件  

关闭文件

打开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作,

ofstream outFile("out.txt");
outFile<<"hello world!"<<endl;
outFile.close();

代码示例:

#include <iostream>
#include <fstream>
#include <string>std::string rand_str(int len)
{std::string str = "";int i;for (i = 0; i < len; ++i){switch ((rand() % 3)){case 1:str += ('A' + rand() % 26);break;case 2:str += ('a' + rand() % 26);break;default:str += ('0' + rand() % 10);break;}}return str;
}int main0()
{//随机生成字符串,写入txt中std::ofstream outFile("out.txt");if (!outFile){std::cout << "File open error!" << std::endl;return false;}srand(time(NULL));for (int i = 0; i < 10; i++){std::string str = rand_str(10);outFile << str << std::endl;}outFile.close();system("pause");return 0;
}int main()
{//从txt中读入字符串std::ifstream inFile("out.txt");if (!inFile) {std::cout << "Unable to open file" << std::endl;return -1;}std::string str;while (getline(inFile, str)){std::cout << str << std::endl;}inFile.close();system("pause");return 0;
}

当从txt中读取字符出的时候,用到了getline的函数,需要加上<string>的头文件。

参考链接:

C++ ofstream和ifstream详细用法_c++ ofstream write 保存视频文件怎么指定帧率_Happinesspills的博客-CSDN博客

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

相关文章:

  • 使用rkhunter检测Linux的rootkit
  • jdk源码写过注释后debug提示source code does not match the bytecode
  • nodejs中的__filename和__dirname的使用说明
  • UIE: 信息抽取的大一统模型
  • 推荐使用:jQuery FullScreen 插件 —— 让全屏模式优雅而简单
  • 钓鱼邮件从入门到放弃
  • SWT简介
  • 查看占用网速的程序
  • 【LINUX】linux中修改文件命名
  • isalpha()方法介绍
  • Oracle数据库的下载安装教程
  • 在虚拟机上安装Ubuntu系统(详细附图)
  • 系统流程图
  • 蓝牙技术简介
  • 1分钟带你理解pairwise
  • 设计原则之贰——里氏替换原则
  • C语言中静态变量的作用
  • 开发自己的web搜索引擎——MyGoGo
  • 文本相似度检查实现
  • Codeforces Round #168 (Div. 2) B. Convex Shape(BFS || 模拟)
  • [19/03/16-星期六] 常用类_Date时间类DateFormat类
  • LC滤波器设计学习笔记(一)滤波电路入门
  • adb bugreport :查看设备所有信息(获取错误报告)
  • android gallery3d 源码分析(一)
  • Winpcap的安装使用方法
  • 【论文笔记】Progressive Layered Extraction (PLE): A Novel Multi-Task Learning (MTL) Model for Personalized
  • iptables/netfilter/iproute2/ip/tc/qdisc经常被混淆的几个概念
  • 调色盘管理器
  • 熊猫烧香专杀工具的编写
  • 鱼哭了,水知道。我哭了,谁知道