istream, outstream使用及常见错误
来源:互联网 发布:程序员工具三宝 编辑:程序博客网 时间:2024/05/22 01:35
使用方法:
使用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.outIn 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
2 0
- istream, outstream使用及常见错误
- Coverity的安装使用及常见错误
- GitHub使用教程及常见错误解决
- GitHub使用教程及常见错误解决
- Zxing使用及常见错误(iOS)
- CocoaPods 安装 使用及常见错误
- git使用常见错误及修复
- FindBugs 使用 及 常见错误解决方法
- 使用ffmpeg常见的错误及解决方案
- oracle安装及使用常见错误
- Mybatis使用常见错误及原因
- Eclipse的常见使用错误及编译错误 && 插件
- Eclipse的常见使用错误及编译错误
- istream
- IStream
- IStream
- 常见错误及解决方法
- 常见错误及解决方法
- Azure Backup 简介
- 使用jquery jcrop插件进行图片的截取与保存
- 守护线程总结
- CentOS系统(四)——gitlab搭建及使用
- mysql重装说明
- istream, outstream使用及常见错误
- JAVA容器
- 最具价值CTO评选
- http://qinxuye.me/article/introduction-to-neo4j/
- C#开发Android应用实战
- 八种排序算法总结
- SQL SERVER – Get Last Running Query Based on SPID
- yv12转nv12,nv12转I420
- KendoUI:Grid控件的使用