vectors to files

hello,, my first post here..
im a 1st year CS student..

i need a help on having the idea of how to make this assignment..
i need to make a program,,a question bank,, with this functions:

1. Add a question
2. Delete question(s)
3. Modify a question
4. Display question(s)
a. All questions
b. Questions by subject
c. Questions by subject-level (category)
5. Search for a question by keyword (displays questions that have the keyword found in the question or answer)(THERE ARE 7 SUBJECTS ad 5 LEVELS,, so 35 CATEGORIES all in all)

The questions database must be persistent across usage sessions. That is to say, changes made to the Bank will have to be retained once the program is closed and then re-opened. To make this possible, the filesystem is used to store the questions for the quiz game. Load it once at startup, then save before quitting. Also, the Bank must be able to store an unlimited number of questions.

is it wise to use vectors here, then store them to files? if so,, how?
i think its easier to manipulate many textfiles,, instead of one,, maybe 1 textfile per subject..

my attempt for now is to read all the questions from a file into a vector,, manipulate the vector,, then clear the file and rewrite to the file everything in the vector...

my problem here is how to the questions themselves.. some questions are multiple choice type,, so there are \n's inside the question,, i can't simply put '\n' as a delimiter.. and that makes storing them to vectors harder..

i'm not that good in file processing and manipulations(like my english!).. so i really need help how to do this one... even without codes,, just specific ideas and algorithms.... if you have alternative methods,, they will help..
thX..
What variable type are your question stored in?
If they are tored in a class, you can overload the << >> operators for file streams.
I think a list would be better than a vector for your problem.
http://www.cplusplus.com/reference/stl/list/
thx!

->string type..
->im not that good in using classes,, so i haven't consider that one yet.. but il try
-> a linked list? i don't know how to store it in a file,, cause all are pointers..

thx

With strings you won't have any problem in storing them using file streams.
As a separator you can use some special characters you are shure would never be used in your strings (eg '\1')
To store from a list to a file use iterators:
1
2
3
4
5
list<string> questions;
//...
ofstream myfile("save");
for ( list<string>::iterator i=questions.begin() ; i != questions.end(); i++ )
      myfile << *i << '\1';
For reading use getline(myfile,temporary_string,'\1');
Last edited on
Topic archived. No new replies allowed.