zoukankan      html  css  js  c++  java
  • istream, outstream使用及常见错误

    使用方法:

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

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

    code:

    [cpp] view plain copy
     
     在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.   
    9. void parsefile(istream& fi, ostream& fo){  
    10.     string tmp;  
    11.     while(fi>>tmp){  
    12.         fo<<tmp<<endl;  
    13.     }  
    14. }  
    15.   
    16. void process(){  
    17.     std::filebuf fbOut,fbIn;  
    18.     fbOut.open("out.txt",std::ios::out);  
    19.     std::ostream fw(&fbOut);  
    20.   
    21.     // Basic function testing  
    22.     fbIn.open("test.cpp",ios::in);  
    23.     istream is(&fbIn);  
    24.     parsefile(is,fw);  
    25.   
    26.     // get all files in this directory and print it to "out.txt" iteratively  
    27.     DIR* dir;  
    28.     struct dirent* ent;  
    29.     if((dir = opendir("deseq"))!=NULL){  
    30.         while((ent = readdir(dir))!=NULL){  
    31.             char* filename = ent->d_name;  
    32.             if (fbIn.open(filename,ios::in)){  
    33.                 istream is(&fbIn);  
    34.                 parsefile(is,fw);//read from is and write in fw  
    35.                 fbIn.close();  
    36.             }  
    37.         }  
    38.     }  
    39. }  
    40.   
    41. int main(){  
    42.     process();  
    43.     return 0;  
    44. }  

    其中,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

  • 相关阅读:
    几款JS地图插件比较
    Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。
    学习嵌入式—导火线
    Linux MySQL 5.1源码安装
    QT 一些非常常用的操作
    QT 下把编辑框内的中文字符转换为 char*
    delphi datasnap 心跳包
    ddd
    Qt 如何处理密集型耗时的事情(频繁调用QApplication::processEvents)
    Python基础-输入输出(IO)
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5354601.html
Copyright © 2011-2022 走看看