C++ Newbie: Passing an fstream to a function to read data
- by vgm64
I have a text file named num.txt who's only contents is the line 123. Then I have the following:
void alt_reader(ifstream &file, char* line){
file.read(line, 3);
cout << "First Time: " << line << endl;
}
int main() {
ifstream inFile;
int num;
inFile.open("num.txt");
alt_reader(inFile, (char*)&num);
cout << "Second Time: " << num << endl;
}
The output is:
First Time: 123
Second Time: 3355185
Can you help me figure out how to get an fstream that is read in a function still assign the variable in main? I'm doing this because alt_reader really has a lot more to it, but this is the part I'm stuck on. Thanks a lot for the help.