return type for file

Hi!

Is it possible to return a file in the return type of a function


1
2
3
4
5
6
7
8
9
10
11
12
13

int main(){
  ifstream fil = fileReturn();
  
  return 0;
}

ifstream fileReturn(){
          ifstream myFile("abc.txt", ios::in);
          if(myFile.is_open()){
          return myFile;
    }
}


is it possible?

thanks before hand :)

Sincere Regards,
You could always try compiling it and see for yourself....

But either way, no, it's not possible since std::ifstream doesn't have a copy constructor.

So you'll have to use something like
1
2
3
4
5
6
7
8
9
10
void getFile(ifstream& out)
{
    out.open("abc.txt");
}
int main()
{
    ifstream myFile;
    getFile(myFile);
    // Do stuff with 'myFile'
}
Topic archived. No new replies allowed.