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.