Hello there ^^
I was wondering how I could read a text file with multiple data structures(not sure if it's called that way) into a matrix(typedef std::map<std::string, std::vector<std::string>>
matrix) and header(typedef std::map<std::string, short>
header) without creating additional ones.
Google didn't work any magic for me today. (Wasn't sure if I should post in beginners or here)
Part of the text file (they're really big):
ID X Y TEXT
Cat 30 230 Chen
Fox 280 230 Ran
Youkai 280 70 Yukari
START END LABEL CREATENEW
Cat Fox Affection 0
Cat Youkai Affection 0 |
I am able to read
into a header for my matrix, but I don't want to read
START END LABEL CREATENEW |
into a new header/matrix, I keep that information in the same text file so I don't get swarmed with text files. (The structures are seperated by one empty line btw)
I was wondering if it's possible to put
ID X Y TEXT START END LABEL CREATENEW |
together after eachother in the same header making it possible to read the data underneath it.
I minimized the amount of code to demonstrate what I currently use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
typedef std::vector<std::string> row;
typedef std::map<std::string, row> matrix;
typedef std::map<std::string, short> header;
using namespace std;
string f;
string getData(/*HWND hwnd, */header &index, matrix &data)
{
/*while (f.empty()) f = DoFileOpen(hwnd);*/ //function to select a file
f = "file.txt";
ifstream file(f);
if (file.is_open())
{
string line;
bool ff = true;
while (getline(file, line))
{
istringstream linestream(line);
string label;
getline(linestream, label, '\t');
string field;
short column = 0;
while (getline(linestream, field, '\t'))
{
if(ff) {
index[field] = column;
column++;
} else {
data[label].push_back(field);
}
}
ff = false;
}
}
return f;
}
|
I can easily access the data I want with
data["Cat"][index["TEXT"]];
to get "Chen". So I'm wondering if it's possible with the same matrix and header to do stuff like
data["Cat"][index["LABEL"]]
= "Affection" and if it's safe to use an iterator when there are two of the same START names.
~ Rii