Jump to content

Storing Data?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hey codecall, I'm creating a Quiz Generator program that takes user defined data and formats it into a quiz format. The problem is I need a way to save data, preferably strings so that they can be reaccessed later. I was wondering what library would be recomended for doing this, I was looking at using the fstream library to save text to a .txt document. This would require a bit of work for reading the input and then restoring the data in a vector, and I was wondering if there is a simpler way to store data. Thanks.

fstream library: Input/Output with files

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 762 posts
Couldn't you use database? Logically database is the first candidate, since I believe you have to add some properties to the stored questions for faster retrieval later. For example, you may want to categorize the questions, or you may want to weight them (like easy, medium, and difficult).

#3
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Yes, the most simplest way is to use files. You can use fstream/FILE for file io.

Yes, you can even use an embedded database; this also saves the data into files, but you can access data through SQL. For example, SQLITE is an embedded database, that you would like to use in your application. More details are

SQLite Home Page

this is free to use.

If you want to use a free relational database, you may want to look at MySQL.
MySQL :: The world's most popular open source database

I hope this helps!
Munir

#4
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I forgot about SQLITE, Im familiar with it in Lua. I will look into it I doubt its very complex, and that way everything can be stored in a single database versus multiple files. I do have web hosting so I could use MySQL but I dont want this program to be web based. Thanks for the input, I will go research SQLITE.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

espdev-darkness said:

will go research SQLITE.
There's a nice looking C++ wrapper here: Sqlite wrapped: a C++ wrapper for the Sqlite database C API

The syntax would look something like this with it, it looks a lot more simple than sqlite's wrapper:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>

#include "Database.h"
#include "Query.h"

int main()
{
	Database db( "database_file.db" );
	Query q(db);

	q.execute("delete from user");
	q.execute("insert into user values(1,'First Person')");
	q.execute("insert into user values(2,'Another Person')");

	q.get_result("select num,name from user");
	while (q.fetch_row())
	{
		long num = q.getval();
		std::string name = q.getstr();

		printf("User#%ld: %s\n", num, name.c_str() );
	}
	q.free_result();
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Im a little confused as to what a wrapper is... From googling it I have found out very little.

*edit: For version one of this program it looks like I will just use fstream, file i/o is all I need the formating isn't to complex. I found an excellent tutorial here: fstream Tutorial - C++.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users