(C ++) Need help with the database
I'm trying to create a database and so far I've used strings to store my entries from a text file to an array, but that just doesn't work. So I started thinking about a new way of doing this.
What I want to do:
Suppose I have a text file with the following database ...
John Smith 00001 jsmith @email pw1
Rob Deniro 00002 rdeniro @email pw2
Al Pacino 00003 apacino @email pw3
Joe Pesci 00004 jpesci @email 307 pw4
Joaq Phoenix 00005 jphoe @email 208 pw5
John Madden 00006 jmadden @email 708 pw6
Ok, so basically what I'm stuck with is making this "inheritance" friendly. What's the best way to store each record? Individual lines? I thought the best way is to store each individual character until the space appears and then store it to a string, but I'm not sure how this can be done.
As TomWij says, you do an ifstream then strtok, but I would recommend that you avoid your strings with "and not just spaces, this way you can store" something like this, like a user note "that how it's done with CSV (Comma Separated Values).
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
std::string csvLine;
// read every line from the stream
while( std::getline(input, csvLine) )
{
std::istringstream csvStream(csvLine);
std::vector<std::string> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while( std::getline(csvStream, csvElement, ',') )
{
csvColumn.push_back(csvElement);
}
output.push_back(csvColumn);
}
}
int main()
{
std::fstream file("file.csv", ios::in);
if(!file.is_open())
{
std::cout << "File not found!\n";
return 1;
}
// typedef to save typing for the following object
typedef std::vector< std::vector<std::string> > csvVector;
csvVector csvData;
readCSV(file, csvData);
// print out read data to prove reading worked
for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
{
for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
{
std::cout << *j << ", ";
}
std::cout << "\n";
}
}
Which brings me to a real solution, why don't you use a CSV library like CSV module or better yet, SQLite , SQLite is dead easy to install and use and all around is better than coding the database by hand, besides it shouldn't you more than 1 hour to get SQLite into its codebase as its API is REALLY lightweight.
#include <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 ){
fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
exit(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
source to share
Read one line at a time with ifstream
and then use strtok
to separate each line, use a space as a separator. You should use string
except for numeric values, which you can store as int
or long
if you need to support large values. It also makes more sense to keep your passwords encrypted if you want to keep them secure.
You might want to find another solution to store your data instead of plain text for various reasons: space, performance, security ... It might be a good idea to try using binary database files.
source to share
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main(int argc, char *argv[])
{
char Name[100];
char FTE[100];
cout<<"What is the file name?\n";
cin>>FTE;
ifstream myfile (FTE);
while(1)
{
myfile.getline(Name, 30, '|');
cout<<line;
cin.ignore();
}
}
all this makes reading all your text entries separated by '|' Symbol. Good for creating file-based databases in C ++ and others using the same techniques.
source to share