Reading from file, getting specific strings

I am currently working on a project where I have to write the contents of string arrays "question" and "answer" to a file. I have finished this function and I am currently working on making a function to reverse this process, taking the text from the file that was written, and creating two arrays question and answer, and filling them with the data provided in the data file.

Currently, my layout for the data file is as follows:

1
2
3
4
5
6
7
8
9
10
3 //number of elements contained in the array.

question one                //goes in question[1]
answer to question one      //goes in answer[1]

question two                //goes in question[2]
answer to question two      //goes in answer[2]

question three              //goes in question[3]
answer to question three    //goes in answer[3] 


So after loading this file, I should have two arrays (question, answer) with three filled elements. (question[1,2,3], answer[1,2,3]) The problem is I can't figure out how to pull only the first line, then skip the newline, pull two lines, skip newline, etc.

Here is my saving function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void save_utils()
{
	string filename_save;
	string extension = ".card";

	cout << "Please enter name of file to save." << endl << endl;
	getline(cin, filename_save);

	filename_save.append(extension);

	ofstream card_export;
	card_export.open(filename_save.c_str());

	if ( !card_export ) 
	{ 
		cerr << "Improper File Stream (saving)\nLine --; save_load_utils.h";
		return_error();
	}

	card_export << (count - 1) << endl << endl;

	for (int i = 1; i < count; i++)
	{
		card_export << question[i] << endl << answer[i] << endl << endl;
	}

	card_export.close();
}


Saving works flawlessly, it's loading I can't get.

I'd really appreciate any help, and no, this is not school or paid work.
Last edited on
You can use the operator >> and insert the contents in some kind of container of strings.


Jeff
Last edited on
Thanks, but that's really not specific enough.
Heh...
How about getline then?
1
2
for(int i = 1; !file.eof(); i++)
	getline(target, data[i]);


here data was a stl container map<int, string>, i used that to get the contents of a file with line numbers, I am sure you can adapt this to any other conteiner you wish even arrays. make sure file exists and it has some contents or you gonna have a nice "for ever".

hmm wonder if i was specific enough this time.

:D

regards,

Jeff
Last edited on
That is very close to what I need, thanks.

How did you use getline to pull one line at a time though?
I don't think i could explain better why it works that way than this:
http://www.cplusplus.com/reference/string/getline.html

If you still have any questions though feel free to ask.

Jeff
Well I need to know how to extract a full specified line. For instance, I want to capture the first line, an integer. getline(card_import, count) is going to try to capture the entire text file (as far as I know) and assign it as the value of count. I want a specific line, and I still don't know how to do that.
Topic archived. No new replies allowed.