Sending file stream to a function.

Hello, everyone. I'm new with this type of topic. So I need your help.

Suppose I have these two functions-
1
2
3
4
5
6
7
8
9
10
11
  void fo(ofstream& o)
{
    o << " Sarker.";
}

void fi(ifstream& i)
{
    string temp;
    i >> temp;
    cout << " " << temp;
}


And in main function, if do this-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ofstream fout("_.txt", ios::out | ios::binary);

    fout << "Shuvo";
    fo(fout);

    fout.close();

    ifstream fin("_.txt", ios::in | ios::binary);
    
    string temp;
    fin >> temp;
    cout << temp;
    fi(fin);

    fin.close();


It works okay. But, only when I receive a reference. That is, if I change the two functions to these- void fo(ofstream o); and void fi(ifstream i); , I get a compile-time error. Why is this?
And besides, what else do I need to know about sending a file stream to a function?
Last edited on
> get a compile-time error. Why is this?

Standard file streams are not CopyConstructible http://en.cppreference.com/w/cpp/concept/CopyConstructible

> what else do I need to know about sending a file stream to a function?

File streams are MoveConstructible http://en.cppreference.com/w/cpp/concept/MoveConstructible

In brief: Pass by value to transfer ownership of the stream to the function. Pass by reference otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <vector>

void pass_by_reference( std::ifstream& stm ) { std::cout << stm.rdbuf() << "\n\n\n" ; }

void pass_by_value( std::ifstream stm ) { std::cout << stm.rdbuf() << "\n\n\n" ; }

int main()
{
    {
        std::ifstream file(__FILE__) ;
        pass_by_reference(file) ; // fine
    }

    pass_by_value( std::ifstream(__FILE__) ) ; // fine, rvalue: moved

    // pass_by_reference( std::ifstream(__FILE__) ) ; // *** error: rvalue (reference to non-const required)

    {
        std::ifstream file(__FILE__) ;
        // pass_by_reference(file) ; // *** error: file streams are not copyable
        pass_by_value( std::move(file) ) ; // fine: file streams are moveable
                                           // but do not use the object 'file' after this: it has been moved
    }

    {
        std::vector<std::ifstream> my_files ;
        my_files.push_back( std::ifstream(__FILE__) ) ; // fine: the vector owns the file now

        std::ifstream file(__FILE__) ;
        // my_files.push_back(file) ; // *** error:
        my_files.push_back( std::move(file) ) ; // fine; ownership is transferred
    }
}
Last edited on
I don't understand!!! My compiler cannot compile your code! It is showing the same error for this line of code- pass_by_value( std::ifstream(__FILE__) ) ; on line 16, which you said would be 'fine'. (and also a bunch of errors)

Is there something to do with the compiler or version of C++? I'm using Code Blocks. It supports some features of C++11 though.
> Is there something to do with the compiler or version of C++?

Yes. The GNU implementation of the standard C++11 library before GCC 5.1 was thoroughly broken in many places. Use GCC 5.1 or later.
http://coliru.stacked-crooked.com/a/65c3f9e1bca89a60

GCC 5.1 with CodeBlocks: http://www.cplusplus.com/forum/beginner/149018/#msg780212
How little I know!
Thanks, however...
Last edited on
Topic archived. No new replies allowed.