I want to get data from a stream that my function returned, but it throws an exception when I try to. Here is my code in short form:
#include <iostream>
#include <fstream>
using namespace std;
ifstream& detect() {
ifstream stream;
stream.open("in.txt");
return stream;
}
int main() {
int s;
ifstream& fin = detect();
fin >> s; //exception thrown: Access violation reading
cout << s;
}
How can I solve this problem? Is it caused because of the reference type of the "fin"?