samedi 3 mars 2018

How can I write the contents of a file to a pointer to an istream?

I'm building a lexical analyzer. I need to get input from a file and pass it into a function that will then read that input. It needs to be a pointer to an istream.

This is the function's calling signature:

Token getToken(istream *in, int *linenumber);

I know that I can convert an fstream into a istream, but when I try to convert an fstream to an istream*, I get an error.

This piece of code builds but then crashes when I run it:

ifstream *infile;
infile->open("text.txt");
char ch;

while( infile->get(ch) ) {
    cout << ch;
}

infile->close();

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire