C++ ifstream

  • std::ifstream
  • constructor
  • check status
  • close
  • TensorRT中的例子
  • 參考連結

std::ifstream

參考std::ifstream:

Input stream class to operate on files.Objects of this class maintain a filebuf object as their internal stream buffer, which performs input/output operations on the file they are associated with (if any).File streams are associated with files either on construction, or by calling member open.

std::ifstream這個類別的物件會對與它關聯的檔案進行操作。

constructor

其建構子的簽名:

/*default (1)*/
ifstream();
/*initialization (2)    */
explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in);
/*copy (3)  */
ifstream (const ifstream&) = delete;
/*move (4)  */
ifstream (ifstream&& x);

在此著重關注第(2)種初始化方式:第一個參數為檔名,第二個參數為檔案開啟模式。

可選的開啟模式包括std::ios_base::openmode的五個型別:inoutbinaryateapptrunc。這些模式可以用bitwise OR operator(|)來進行組合。

check status

參考ifstream: check if opened successfully,為了檢查檔案是否開啟成功,我們可以使用對std::ifsstreamoverload過的!運算子。

close

std::ifstream::close函數的簽名:

void close();

其作用為:

Close file
Closes the file currently associated with the object, disassociating it from the stream.

即關閉檔案,取消它與input stream的關聯。

TensorRT中的例子

TensorRT/parsers/caffe/caffeParser/readProto.h的函數readBinaryProto中。

創建ifstream物件:

std::ifstream stream(file, std::ios::in | std::ios::binary);

注意到這裡使用的std::iosstd::ios_base的子類別。

檢查檔案是否開啟成功:

if (!stream)
{RETURN_AND_LOG_ERROR(false, "Could not open file " + std::string(file));
}

關閉檔案:

stream.close();

參考連結

std::ifstream

ifstream: check if opened successfully

C++ ifstream相关推荐

  1. 在使用stl中的ifstream出错时如何快速排错?

    工作中编写demo,解析配置文件,为了便捷, 使用了ifstream函数, 通常我都是这么用的,一般也很少出错, 但是这次确出错了,找了很久才排查出问题. 原来是配置文件写错了.特此记录一下心得. 快 ...

  2. [转载]VS2005中ifstream不能读中文路径文件的bug

    这几天一直被题目中问题缠绕着,在网上找到很多答案,实际能解决问题的答案只看到一个: 在程序中加入: std::locale loc = std::locale::global(std::locale( ...

  3. c语言打开文件出现分段故障,我不明白为什么我使用ifstream时出现分段错误

    我对C++相当陌生.我试图打开一个文件并将其传递给另一个方法,以便我可以从ifstream读取数据.这是打开文件的方法.我不明白为什么我使用ifstream时出现分段错误 int main() { / ...

  4. c++ ifstream 文件不结束_C++核心编程 第十二节 文件操作

    前言:程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放.通过文件可以将数据持久化. C++中对文件操作需要包含头文件 ,C++中操作文件有不同的文件打开方式,具体见下表所示. 文件打开方 ...

  5. 【c++基础】ifstream的构造函数

    公共成员函数: default (1) ifstream(); initialization (2) explicit ifstream (const char* filename, ios_base ...

  6. ifstream java_C ifstream将读取一些值然后停止

    我正在尝试编写一个程序,从bin文件中读取940个4字节长的二进制数据值[hex],并将值输出到控制台 . 我在循环中有ifstream :: read,cout和seekg操作 . 它将适用于前10 ...

  7. 【C++ 语言】文件操作 ( fopen | fprintf | fscanf | fgets | fputc | fgetc | ofstream | ifstream )

    文章目录 I C 函数 fopen 打开文件 II C 函数 fprintf 写出文件 III C 函数 fscanf 读取文件 ( 遇到空格换行结束) IV C 函数 fgets 读取文件 ( 遇到 ...

  8. c++中ifstream一次读取整个文件

    转载:http://www.cnblogs.com/kex1n/p/4028428.html 第一种方法: 读取至std::string的情况: #include <string> #in ...

  9. ifstream eof()函数

    用ifstream的eof(),竟然读到文件最后了,判断eof还为false.网上查找资料后,终于解决这个问题. 参照文件:http://tuhao.blogbus.com/logs/21306687 ...

  10. ofstream、ifstream、fstream

    文章目录 1 文件读写相关的流 1.1 文件读写相关的流 1.2 文件打开方式 2 ofstream.ifstream.fstream 2.1 ofstream和ifstream读写文本文件 2.2 ...

最新文章

  1. SilverLight 双向绑定
  2. python 启动django时报错MySQLdb._exceptions.OperationalError: (2059, <NULL>)和django.db.utils.OperationalEr
  3. python websocket server_Python Websocket服务端
  4. opencv 滤镜效果php,OpenCV实现马赛克和毛玻璃滤镜效果
  5. 【今日CS 视觉论文速览】 25 Jan 2019
  6. Activate Windows server 2003 in VMware
  7. Linux系统下QT九宫格拼图小游戏
  8. POJ 3345 Bribing FIPA(树形dp+背包)
  9. python设置桌面歌词_Python生成歌词词云
  10. Makefile文件
  11. nest keyword_Alexa仍将与Nest合作(这是一个问题)
  12. MySQL基本数据类型与Java基本数据类型
  13. 美国证监会暂停香港设立的区块链公司的股票交易
  14. Mentor-dft 学习笔记 day12-Multi-Mode ChainsScan Insertion Flows
  15. VMware Centos7 NAT 环境配置(镜像源+静态IP)
  16. Hive建表(分区表、非分区表)实例
  17. 汇编语言程序教程:从入门到精通!
  18. HTTP 协议相关入门
  19. 【高等数学】下册 第十二章 第一节 常数项级数的概念和性质
  20. Java做的毕业设计:图形化页面单机版————电脑内存小助手

热门文章

  1. PHP判断金花中的散牌(杂六)、对子、半顺、顺子、豹子,比较简单的算法
  2. 考研初试占比高,复试压力小院校合集~
  3. bugfree配置和使用
  4. [AST基础篇]Scope作用域详解
  5. 微软Surface Go 3 评测
  6. I.MX 启动方式 - DCD数据详解
  7. Layui表单验证失效解决办法
  8. 全球新闻数据可视化(1)--数据下载与处理
  9. hog特征提取python代码_HOG特征提取
  10. Kevin Mitnick 小故事