Storing structured data in the Hard Disk

Hi,
I need to have a data structure(container) such as vectors or arrays where I can store data in the memory as well as in the Hard disk. In the following code where I'm writing my fullNames (std::vector<std::vector<std::string>>) in the HDD by making two separate DAT files. I was wondering if there is a better way of doing that. Thanks.

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
47
48
49
#include <iostream>
#include <vector>
#include <string>
#include <iterator>     // std::ostream_iterator
#include <fstream>

void writeToDat()
{
	std::vector<std::vector<std::string>> fullNames{ { "Max", "Wild" }, {"Ellie", "Olson"} }; //main file where data is stored 
	std::vector<std::string> firstNames{}; 
	std::vector<std::string> lastNames{};
	for (int i = 0; i < fullNames.size(); i++)
	{
		firstNames.push_back(fullNames[i][0]); //filling up firstname 
		lastNames.push_back(fullNames[i][1]); //filling up lastname
	}
	std::ofstream ofs_first, ofs_last;
	ofs_first.open("firstnames.dat", std::ios::out | std::ios::binary);
	ofs_last.open("lastnames.dat", std::ios::out | std::ios::binary);
	std::ostream_iterator<std::string> o_iter_first(ofs_first, "\n");
	std::ostream_iterator<std::string> o_iter_last(ofs_last, "\n");
	std::copy(firstNames.begin(), firstNames.end(), o_iter_first);
	std::copy(lastNames.begin(), lastNames.end(), o_iter_last);
	ofs_first.close();
	ofs_last.close();
}

void readDatFile()
{
	std::vector<std::vector<std::string>> fullNames{};
	std::vector<std::string> firstNames{};
	std::vector<std::string> lastNames{};
	std::ifstream ifs_first("firstnames.dat");
	std::ifstream ifs_last("lastnames.dat");
	std::string tmp;
	while (std::getline(ifs_first, tmp))
	{
		firstNames.push_back(tmp);
	}
	ifs_first.close();
	while (std::getline(ifs_last, tmp))
	{
		lastNames.push_back(tmp);
	}
	ifs_last.close();
	for (int i = 0; i < firstNames.size(); i++) fullNames.push_back({ firstNames[i], lastNames[i] }); //recreating fullnames
	
	for (std::string i : fullNames[1]) std::cout << i << std::endl;
}
Last edited on
I think you are overcomplicating it.

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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct FullName {
    string last;
    string first;
    FullName(string lst, string frst) : last(lst), first(frst) {}
};

void writeNames(vector<FullName>& names) {
    ofstream ofs("names.dat");
    for (const auto& x: names)
        ofs << x.last << ", " << x.first << '\n';
}

void readNames(vector<FullName>& names) {
    ifstream ifs("names.dat");
    string line;
    while (getline(ifs, line)) {
        istringstream iss(line);
        string first, last;
        getline(iss, last, ',');
        iss.get();
        getline(iss, first);
        names.push_back(FullName(first,last));
    }
}

int main() {
    vector<FullName> names {
         {"Wild" , "Max"},
         {"Olson", "Ellie"}
    };
    writeNames(names);
    names.clear();
    readNames(names);
    for (const auto& x: names)
        cout << x.first << " : " << x.last << '\n';
}

Last edited on
@tpb thanks. I came up with my way as I didn't know anything else at that moment. However, ur way is smarter.
Topic archived. No new replies allowed.