So basically I'm supposed to make a database like application in c++. The problem I have is when I try to save/read from the .txt files I've created. Classes definitions and output functions:
file:table.h
#ifndef TABLE_H__
#define TABLE_H__
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include<cstdio>
using namespace std;
class tableColumn
{
public:
string colName;
vector<string> tableCol;
tableColumn() {};
~tableColumn() {};
string getColName() { return colName; }
void setColName(string);
int getColSize() { return tableCol.size(); }
};
void tableColumn::setColName(string a)
{
colName = a;
}
class tableMatrix
{
private:
vector<tableColumn> table;
string tableName;
public:
tableMatrix() {};
~tableMatrix() {};
string getTableName() { return tableName; }
void setTableName(string);
void saveToFile(string);
void readFromFile(string);
};
void tableMatrix::saveToFile(string tableFileName)
{
string aux;
aux += tableFileName;
aux += ".txt";
ofstream out(aux.c_str());
out << tableName << endl;
out << table.at(1).getColSize() << endl;
out << table.size() << endl;
for (int i = 0; i < (int)table.size(); i++)
{
out << table.at(i).colName<<endl;
for (int j = 0; j < (int)table.at(i).tableCol.size(); j++)
{
out << table.at(i).tableCol.at(j) << endl;
}
}
out.close();
}
void tableMatrix::readFromFile(string tableFileName)
{
string aux,element,space;
int rows, cols;
aux += tableFileName;
aux += ".txt";
ifstream in(aux.c_str());
tableColumn auxc;
getline(in, tableName);
in >> rows;
in >> cols;
for (int i = 0; i < cols; i++)
{
in >> auxc.colName;
for (int j = 0; j < rows; j++)
{
in >> element;
auxc.tableCol.push_back(element);
}
table.push_back(auxc);
auxc.~tableColumn();
}
in.close();
}
#endif
file:database.h
#ifndef DATABASE_H__
#define DATABASE_H__
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include"table.h"
#include<cstdio>
using namespace std;
class database
{
private:
vector<tableMatrix> db;
public:
database() {};
~database() {};
void saveToFile();
void readFromFile();
};
void database::saveToFile()
{
ofstream out("tablenames.txt");
for (int i = 0; i < (int)db.size(); i++)
{
out << db.at(i).getTableName() << endl;
}
out.close();
vector<string> filenames;
string aux;
for (int i = 0; i < (int)db.size(); i++)
{
aux = db.at(i).getTableName();
filenames.push_back(aux);
}
for (int i = 0; i < (int)db.size(); i++)
{
db.at(i).saveToFile(filenames.at(i));
}
}
void database::readFromFile()
{
vector<string>filenames;
string aux;
ifstream in("tablenames.txt");
tableMatrix auxt;
while (!in.eof())
{
in >> aux;
filenames.push_back(aux);
}
for (int i = 0; i < (int)filenames.size(); i++)
{
auxt.readFromFile(filenames.at(i));
db.push_back(auxt);
}
in.close();
}
#endif
main.cpp:
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include"database.h"
#include<sstream>
#include<Windows.h>
#include<conio.h>
#include<vector>
using namespace std;
int main()
{
database A;
A.readFromFile();
A.saveToFile();
return 0;
}
file : tablenames.txt
aaa
asa2
file : aaa.txt
aaa
3
2
col1
1
2
3
col2
4
5
6
file : asa2.txt
asa2
3
2
col1
1
2
3
col2
4
5
67
Basically I want to create a txt file with all table names (tablenames.txt) and then a txt file for each table with its name and column data. However when I try to run it, it's duplicating my last entry in tablenames.txt and it messes up my table data by adding additional columns and/or rows.
For example, tablenames.txt : ramdomname1, ramdomname2 becomes: ramdomname1,ramdomname2,ramdomname2. Same deal with the other table files except that it does it on a larger scale (2 rows become 10 for example with duplicated/scrambled data).