C++ Read a whole File using ifstream
来源:互联网 发布:网络维护好学吗 编辑:程序博客网 时间:2024/06/02 03:31
std::ifstream ifs("filename.txt");
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
Mind the extra parenthesis in the str declaration, it's necessary for correct parsing.
it's a string constructor that takes an input iterator pair. The first input iterator is an istreambuf_iterator initialized with the stream. The second input iterator is an istreambuf_iterator is default constructed. A default constructed istreambuf_iterator returns equal to another istreambuf_iterator that has exhausted the input from the streambuf it was reading from. So the constructor reads from the first istreambuf_iterator until it runs out of data.
The first argument needs to go in parenthesis otherwise the compiler will parse the string declaration as a function declaration. Specifically a function that returns a string, called str that takes as a first argument a istreambuf_iterator<char> called ifs and as a second argument an istreambuf_iterator<char> that is unnamed. Technically you can also wrap the second argument in parenthesis instead, but usually when you do it, you wrap the first one.
- C++ Read a whole File using ifstream
- use ifstream read a file
- Read a whole file into a string in C++
- [C++]C++小笔记之Simple File read and write-- ofstream and ifstream
- Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream
- Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert )
- Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert )
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- How to read ini file using Java
- Read a file from xml
- Read file from inside a JAR file
- ABAP Using a Cursor to Read Data
- ifstream(read函数)
- [zz] Read and write a properties file
- Read and Write a /proc File
- Read properties file in a jar
- Read the Last line in a file
- MySQL日志总结
- 预处理(宏定义、文件包含、条件编译)
- ubuntu无法访问win7磁盘
- GCD倒计时
- 浅谈:iOS编程之初级数据持久化
- C++ Read a whole File using ifstream
- Android之——退出多个Activity
- MySQL权限管理总结
- 网页侧边半收藏,鼠标移上时,滑出并全显示
- Tips from the CEO of SNAPCHAT
- 杭电1869六度分离
- 全国首部arcgis for javascript 二次开发视频教程
- 【线段树】 HDOJ 5381 The sum of gcd
- 详解shared_ptr