使用方法:

使用filebuf打开文件,并拷贝给istream/ostream。

如下面的例子中,实现读取并处理deseq文件夹下所有文件,输出到ostream fw.

code:

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<stdio.h>
  5. #include<fstream>
  6. #include<dirent.h>
  7. using namespace std;
  8. void parsefile(istream& fi, ostream& fo){
  9. string tmp;
  10. while(fi>>tmp){
  11. fo<<tmp<<endl;
  12. }
  13. }
  14. void process(){
  15. std::filebuf fbOut,fbIn;
  16. fbOut.open("out.txt",std::ios::out);
  17. std::ostream fw(&fbOut);
  18. // Basic function testing
  19. fbIn.open("test.cpp",ios::in);
  20. istream is(&fbIn);
  21. parsefile(is,fw);
  22. // get all files in this directory and print it to "out.txt" iteratively
  23. DIR* dir;
  24. struct dirent* ent;
  25. if((dir = opendir("deseq"))!=NULL){
  26. while((ent = readdir(dir))!=NULL){
  27. char* filename = ent->d_name;
  28. if (fbIn.open(filename,ios::in)){
  29. istream is(&fbIn);
  30. parsefile(is,fw);//read from is and write in fw
  31. fbIn.close();
  32. }
  33. }
  34. }
  35. }
  36. int main(){
  37. process();
  38. return 0;
  39. }

其中,parsefile的声明为:void parsefile(istream& fi, ostream& fo)

注意这里函数声明必须要加引用,不然会报错:

错误:

$ g++ -g test.cpp -o b.out
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:39,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
                 from test.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:790: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:47: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor 'std::basic_istream<char, std::char_traits<char> >::basic_istream(const std::basic_istream<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:53: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here 
test.cpp: In function 'void process()':
test.cpp:24: note: synthesized method 'std::basic_istream<char, std::char_traits<char> >::basic_istream(const std::basic_istream<char, std::char_traits<char> >&)' first required here

test.cpp:24: error:   initializing argument 1 of 'void parsefile(std::istream, std::ostream)'

原因是stream类型数据不支持拷贝,所以必须传址。

参考:http://stackoverflow.com/questions/6457591/problem-with-ostream-ofstream-inheritance

from: http://blog.csdn.net/abcjennifer/article/details/41545655

istream, outstream使用及常见错误的更多相关文章

  1. 初识JAVA(二)(送给Java和安卓初学者)----常见错误

    博主接着上篇的来讲哦,以后的更新中,博主会出一些练习题,有兴趣的可以做做然后吧代码粘贴到下面,大家可以一起研究学习,一起进步,本篇文章主要讲的是: 一.常见错误 二.连接上篇一起的训练 无论是什么方向 ...

  2. ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock

    ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock 通过终端安装程序sudo apt-get install xxx时出错:E: Could not ...

  3. coreseek常见错误原因及解决方法

    coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...

  4. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  5. C语言初学者代码中的常见错误与瑕疵(23)

    见:C语言初学者代码中的常见错误与瑕疵(23)

  6. struts2.5框架使用通配符指定方法常见错误

    struts2.5框架使用通配符指定方法(常见错误) 在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace ...

  7. .Net常见错误

    常见错误 #1: 把引用当做值来用,或者反过来 C++ 和其他很多语言的程序员,习惯了给变量赋值的时候,要么赋单纯的值,要么是现有对象的引用.然而,在C# 中,是值还是引用,是由写这个对象的程序员决定 ...

  8. WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭

    在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于WCF服务的特殊性,调试起来也不是那么方便,因此往往会花费不少时间来进行跟 ...

  9. Python程序的常见错误(收集篇)

    关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...

随机推荐

  1. 【Simplify Path】cpp

    题目: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&quo ...

  2. UIKit 框架之UISearchController

    // // tableViewController.m // searchController // // Created by City--Online on 15/6/1. // Copyrigh ...

  3. WebServices中使用Session

    默认情况下,Asp.net使用cookie来管理会话状态.因此,Asp.net假设客户端存储了会话cookie并将它与每一个请求一并发回给客户端. /// <summary> /// Su ...

  4. 【CentOS】Eclipse插件egit使用

    1.简介 2.安装 3.配置 4.使用 5.补充说明   参考资料: http://yufenfei.iteye.com/blog/1750124    1.简介     EGit就是一款Eclips ...

  5. bzoj 1497 最小割模型

    我们可以对于消费和盈利的点建立二分图,开始答案为所有的盈利和, 那么源向消费的点连边,流量为消费值,盈利向汇连边,流量为盈利值 中间盈利对应的消费连边,流量为INF,那么我们求这张图的最小割,用 开始 ...

  6. 剑指offer--面试题22

    关键在于思路,  需要两个输入向量,而函数中需要一个辅助栈! 思路:以待判出栈序列为基础,逐个判断它与栈顶元素是否相等,相等则弹出且j++,这表明此元素可为出栈顺序元素,不相等则栈元素不断入栈,直至相 ...

  7. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...

  8. 重定向 vs output redirect

    http://asawicki.info/files/visual_cpp_redirect.png http://asawicki.info/news_1496_redirecting_output ...

  9. HDU1005Number Sequence(找规律)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. ios 7 20像素解决

    1.if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7) { self.view.bounds = CGRectMake( ...