使用ifstream ofstream copy快速处理文本
来源:互联网 发布:不成问题的问题 知乎 编辑:程序博客网 时间:2024/06/10 15:53
c++对文件的操作分成了两种类型,一种是“文本"文件,另一种是"二进制"文件;文本文件是人类能够看懂的,二进制文件是机器文件是我们看不懂的。
这样区分对我们对文件的处理有了更明确的方向,加快了文件的处理和使得代码更加简洁。
如果说我们需要把容器中的数据写到一个流中(本次就是文件流了,其它流也可以),或者是把流中的数据(本次为文件流)写到一个容器中,代码可
以向下面这样简化。
#include <iostream>#include <fstream>#include <iterator>#include <vector>#include <sstream>int main(int argc,char **argv){if (argc != 3) {std::cout << "Usage: " << argv[0] << " <outFileName> <inFileName>" << std::endl;return -1;}std::ofstream outFileStream(argv[1]);std::vector<int> vecInt{ 123,34,231,343,454,343,565,565 };std::copy(vecInt.begin(), vecInt.end(), std::ostream_iterator<int>(outFileStream, " "));outFileStream.close();std::ifstream inFileStream(argv[2]);vecInt.clear();
#if 1
//直接在容器后面插入std::copy(std::istream_iterator<int>(inFileStream), std::istream_iterator<int>(), std::back_inserter(vecInt));
#else
//从vecInt的开始位置覆盖,数量为文件流中int数据的数量与容器大小之间小的那一个;注意如果vecInt限定了容器大小,且大小小于文件流中的int数据量则会
//有不可预料的结果
std::copy(std::istream_iterator<int>(inFileStream), std::istream_iterator<int>(), vecInt.begin());
#endif
//输出到终端
std::copy(vecInt.begin(), vecInt.end(), std::ostream_iterator<int>(std::cout, " "));inFileStream.close();::getchar();return 0;}
另个在VS中这样设置,可以让生成的程序可以直接在windows上运行,不需要附带运行dll(一般为mscvp140.dll,ucrtbase.dll,vcruntime140.dll),我使用的VS是2015
选中工程右键选择属性,选择c/c++中的代码生成,选择运行库为多线程MT(release版本)
阅读全文
0 0
- 使用ifstream ofstream copy快速处理文本
- ifstream、ofstream的使用
- c++文件处理ofstream,ifstream
- fstream ifstream ofstream的使用
- ifstream ofstream 的初始化使用
- ifstream ofstream 的初始化使用
- ofstream & ifstream
- ifstream&ofstream
- fstream(ifstream和ofstream)使用详解
- 使用ifstream ofstream重定向输入输出 c++
- ifstream或ofstream读取二进制文本乱码问题
- ifstream或ofstream读取二进制文本乱码问题
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- [转]: ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- ifstream或ofstream读取二进制文本乱码问题
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- 大数据处理之(top k)
- spring整合hibernate不带有hibernate.cfg.xml(推荐使用)
- bootstrap-select多选框
- 移动端车牌识别技术,基于安卓,ios平台支持二次开发
- UVa 401
- 使用ifstream ofstream copy快速处理文本
- 20171109
- mybatis分页拦截器的实现
- 移植u-boot-2012.04----修改默认参数、烧写yaffs文件
- 【 CodeFors 877 】 A B
- 习题6.4
- 二叉树的重建(3种)---编程之美
- String、StringBuffer与StringBuilder之间区别
- 二叉查找树(BST)