If you're creating pointers to streams like that, you're probably doing something wrong.
This should work fine:
int main()
{
double input;
std::ifstream os("prova.dat");
os >> input;
return 0;
}
Because the ifstream was allocated on the stack, it will be automatically cleaned up when the function finishes, which will close the underlying filehandle. You missed the explicit delete you'd need to do the same in your code.
If you need to pass your stream around, you can do so by reference:
double read_double(std::ifstream& stream)
{
double d;
stream >> d;
return d;
}
int main()
{
std::ifstream os("prova.dat");
double input = read_double(os);
return 0;
}
ifstreams! They're not intended to be used that way. They can be passed by reference where necessary.inputwas a string. Eitheros->operator>>(input)or*os >> inputworks.