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.
UPDATE: Using Bill Oneal's comments, I've written
void alt_reader(ifstream &file, stringstream &str, int n){
char buffer[n+1];
file.read(buffer, n);
buffer[n] = 0;
str << buffer;
cout << "First Time: " << buffer << endl; //First Time: 123
}
int main() {
ifstream inFile;
stringstream strm;
int num;
inFile.open("num.txt");
alt_reader(inFile, strm, 3);
cout << "Second Time: " << num << endl; //Second Time: 123
}
Thanks. Any critiques with what's there now?