Keeping a file open while passing to different functions

Hello, I've only been working with C++ for less than a week and I have a problem. I've written a question class for a trivia game that works fine when I enter data from the keyboard but I want it to be able to get data from a text file.

The problem I am having is that when I open the file to add questions and answers the file keeps getting reset to the beginning of the file whereas I need each line to advance every time getline() is called in the program and getline() is called within different functions.

In short is there a way to pass a file name (or fstream object) as an argument to a function, have said function extract the data it needs and when the same function or another function that uses the file is called the data stream picks up where the other left off. Something like the following:

lets say textfile.txt contains:

What is the capital of Kazakhstan?
1 : Bishkek
2 : Astana
3 : Tashkent
4 : Ashgabat
1
2
3
4
5
6
7
8
9
10
11
int main()
{
   ifstream myFile;
   myFile.open("textfile.txt");
   Question Q;              //An instance of my question class
   Q.getQuestion(myFile);   //Stores a line of data in a C-string
   Q.getAnswers(myFile);    //Stores 4 lines in a string array using a for loop
   Q.giveQuestion();        //Uses cout to give the question
   Q.giveAnswers();         // Uses cout with a for loop to give the answers
   return 0;
}

Whatever I do and I've tried a lot, getAnswers() always resets to the beginning of the file and my output is:

What is the capital of Kazakhstan?
What is the capital of Kazakhstan?
1 : Bishkek
2 : Astana
3 : Tashkent

Any help would be much appreciated you can see how this would be very problematic when multiple questions are part of the file.

Are you passing the ifstream as a reference in the functions?

EDIT: I'd need to see more code (the code for Q::getQuestion and Q::getANswers) to be able to help
Last edited on
In the best result I have had (the one above) I'm passing the actual file name :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{  
	//int c = 0, a;
	//string line;
	//ifstream myfile("trivia.txt");
	//Question Q;
	//getline (myfile,line);
	//Q.getQuestion("trivia.txt");
	//Q.giveQuestion();
	Question trivia[2];
    for (int i = 0; i < 2; i++)
	{
		Question Q;
		Q.getQuestion("trivia.txt");
		Q.getAnswers("trivia.txt");
		//Q.getCorrect();
		//cin.ignore();
		trivia[i] = Q;
	}
	trivia[0].giveQuestion();
	trivia[0].giveAnswers();


Here is the other code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Question::getQuestion(const char * filename)
{   
	ifstream inputFile;
	inputFile.open(filename,ios::in);
	string q;
	//cout << "Enter a question.\n";
	getline (inputFile,q);
	queary = q;
	inputFile.close();
}

void Question::getAnswers(const char * filename)
{
	ifstream inputFile;
	inputFile.open(filename,ios::in);
	string a;
    for (int indx = 0; indx < 4; indx++)
	 {   
		//cout << "Enter a possible answer.\n";
	    getline (inputFile,a);
		ans[indx] = a;
	 }
   
}


Passing the file object is desirable but seems impossible.
I've actually worked it out on my own. I have not yet mastered reference variables or pointers but it seems they are key to everything in c++
Topic archived. No new replies allowed.