istream, outstream使用及常见错误
使用方法:
使用filebuf打开文件,并拷贝给istream/ostream。
如下面的例子中,实现读取并处理deseq文件夹下所有文件,输出到ostream fw.
code:
- #include<iostream>
- #include<stdlib.h>
- #include<string>
- #include<stdio.h>
- #include<fstream>
- #include<dirent.h>
- using namespace std;
- void parsefile(istream& fi, ostream& fo){
- string tmp;
- while(fi>>tmp){
- fo<<tmp<<endl;
- }
- }
- void process(){
- std::filebuf fbOut,fbIn;
- fbOut.open("out.txt",std::ios::out);
- std::ostream fw(&fbOut);
- // Basic function testing
- fbIn.open("test.cpp",ios::in);
- istream is(&fbIn);
- parsefile(is,fw);
- // get all files in this directory and print it to "out.txt" iteratively
- DIR* dir;
- struct dirent* ent;
- if((dir = opendir("deseq"))!=NULL){
- while((ent = readdir(dir))!=NULL){
- char* filename = ent->d_name;
- if (fbIn.open(filename,ios::in)){
- istream is(&fbIn);
- parsefile(is,fw);//read from is and write in fw
- fbIn.close();
- }
- }
- }
- }
- int main(){
- process();
- return 0;
- }
其中,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使用及常见错误的更多相关文章
- 初识JAVA(二)(送给Java和安卓初学者)----常见错误
博主接着上篇的来讲哦,以后的更新中,博主会出一些练习题,有兴趣的可以做做然后吧代码粘贴到下面,大家可以一起研究学习,一起进步,本篇文章主要讲的是: 一.常见错误 二.连接上篇一起的训练 无论是什么方向 ...
- 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 ...
- coreseek常见错误原因及解决方法
coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- C语言初学者代码中的常见错误与瑕疵(23)
见:C语言初学者代码中的常见错误与瑕疵(23)
- struts2.5框架使用通配符指定方法常见错误
struts2.5框架使用通配符指定方法(常见错误) 在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace ...
- .Net常见错误
常见错误 #1: 把引用当做值来用,或者反过来 C++ 和其他很多语言的程序员,习惯了给变量赋值的时候,要么赋单纯的值,要么是现有对象的引用.然而,在C# 中,是值还是引用,是由写这个对象的程序员决定 ...
- WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭
在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于WCF服务的特殊性,调试起来也不是那么方便,因此往往会花费不少时间来进行跟 ...
- Python程序的常见错误(收集篇)
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...
随机推荐
- 【Simplify Path】cpp
题目: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&quo ...
- UIKit 框架之UISearchController
// // tableViewController.m // searchController // // Created by City--Online on 15/6/1. // Copyrigh ...
- WebServices中使用Session
默认情况下,Asp.net使用cookie来管理会话状态.因此,Asp.net假设客户端存储了会话cookie并将它与每一个请求一并发回给客户端. /// <summary> /// Su ...
- 【CentOS】Eclipse插件egit使用
1.简介 2.安装 3.配置 4.使用 5.补充说明 参考资料: http://yufenfei.iteye.com/blog/1750124 1.简介 EGit就是一款Eclips ...
- bzoj 1497 最小割模型
我们可以对于消费和盈利的点建立二分图,开始答案为所有的盈利和, 那么源向消费的点连边,流量为消费值,盈利向汇连边,流量为盈利值 中间盈利对应的消费连边,流量为INF,那么我们求这张图的最小割,用 开始 ...
- 剑指offer--面试题22
关键在于思路, 需要两个输入向量,而函数中需要一个辅助栈! 思路:以待判出栈序列为基础,逐个判断它与栈顶元素是否相等,相等则弹出且j++,这表明此元素可为出栈顺序元素,不相等则栈元素不断入栈,直至相 ...
- Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...
- 重定向 vs output redirect
http://asawicki.info/files/visual_cpp_redirect.png http://asawicki.info/news_1496_redirecting_output ...
- HDU1005Number Sequence(找规律)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- ios 7 20像素解决
1.if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7) { self.view.bounds = CGRectMake( ...