In the following code, what type is examplefile?
I know that FILE is incorrect!
Thanks
Tony
main ()
{
char infile[]="test.it";
ifstream examplefile (infile);
myFunction (examplefile);
return 0;
}
myfunction(FILE ef)
{
/*do something with ef*/
.....
....
}
examplefile is an object of type ifstream. Think of it this way, when
you declare a variable or object, you type: variable_type variable_name;
Compare it to the line above of where you declare examplefile, you
typed: char infile[] = "test.it"; A variable of type char named infile,
the [] tells us that it's an array, the = says that we're going to
define infile right now, and "test.it" is what we're defining infile as.
Your code should look more like:
#include <fstream>
#include <iostream>
using namespace std;
void myFunction(ifstream&);
int main() {
/*char infile[]="test.it"; this isn't necessary*/
ifstream examplefile("test.it");
myFunction(examplefile);
examplefile.close();
return 0;
}
void myFunction(ifstream& ef) {
/*do stuff to the stream, probably read it*/
while(ef.good()) cout << (char)ef.get();
cout << endl;
}
HTH