I'm trying to read in a file one line at a time inside a function that gets called multiple times from main, but I cant seem to get it to stop at the end of a line, or maintain its position in the file between calls. Is there a way to get the file read to maintain its index after the function ends and get it to read in only one line?
1. Declare inFile as static. This ensures that it always has the same address and isn't destroyed when the function ends. The file will remain open between function calls; static ifstream inFile;
2. Make infixIn a member function of <template T> class StackType; and make ifstream inFile an object in the class.
3. Open inFile in main and pass it as a reference to the infixIn function:
1 2 3 4 5 6 7 8 9
int main()
{
ifstream inFile("infix.txt");
infixIn(&infix, inFile);
}
void infixIn(StackType<char> *infix, ifstream &inFile) // Using pass-by-reference instead of pass-by-pointer.
{
}
thanks. I opened it in main and it persists between function calls, but I still cant get it to stop at the end of a line. I thought while (inFile >> in && in != '\n') would stop at the end of each line, but it reads in the whole file and stores it in the stack.
nevermind, I used getline with a string then walked through the string to separate the characters