I Developed some code below which will pull entries from a list and build a series of entries for later use. The list I used and code is below.
Using the ifstream function, I grab characters from the list and every time the code reaches a next line character or \n
, the code will compile the text, build the string and move to the next line.
Is this efficient for pulling from a txt file, It works but feels inefficient.
Text file list
name
title
year
manufacturer
developer
genre
cloneOf
players
ctrltype
buttons
joyways
rating
score
Current code I am trying to optomize
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
std::string sql;
std::string Entryname;
std::string Entryappends;
int main()
{
std::ifstream MetaEntries ("c:\\CodeRepo/List.txt", std::ifstream::in);
sql.append("CREATE TABLE IF NOT EXISTS Meta(");
sql.append("collectionName TEXT KEY,");
char c = MetaEntries.get();
while (MetaEntries.good()) {
if (c == '\n') {
Entryappends = std::string(" TEXT NOT NULL DEFAULT '', ");
sql.append(Entryname + Entryappends);
Entryname.clear();
}
Entryname += c;
c = MetaEntries.get();
// std::cout << Entryname;
}
MetaEntries.close();
Entryappends = std::string(" TEXT NOT NULL DEFAULT '';");
sql.append(Entryname + Entryappends);
sql.append("CREATE UNIQUE INDEX IF NOT EXISTS MetaUniqueId ON Meta(collectionName, name);");
std::cout << sql;
return 0;
}
output: Pretty much it works exactly as intended.
CREATE TABLE IF NOT EXISTS Meta(collectionName TEXT KEY,name TEXT NOT NULL DEFAULT '',
title TEXT NOT NULL DEFAULT '',
year TEXT NOT NULL DEFAULT '',
manufacturer TEXT NOT NULL DEFAULT '',
developer TEXT NOT NULL DEFAULT '',
genre TEXT NOT NULL DEFAULT '',
cloneOf TEXT NOT NULL DEFAULT '',
players TEXT NOT NULL DEFAULT '',
ctrltype TEXT NOT NULL DEFAULT '',
buttons TEXT NOT NULL DEFAULT '',
joyways TEXT NOT NULL DEFAULT '',
rating TEXT NOT NULL DEFAULT '',
score TEXT NOT NULL DEFAULT '';CREATE UNIQUE INDEX IF NOT EXISTS MetaUniqueId ON Meta(collectionName, name);