+ 2

How to store 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. #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; }

20th Apr 2018, 2:20 PM
Flash
15 Answers
+ 2
Not significantly better. But now that you have written your own library functions, isn't it sufficient?
20th Apr 2018, 2:36 PM
Timon Paßlick
+ 2
#include <type_traits> #include <fstream> template <typename T> std::enable_if_t<std::is_trivially_copyable<T>::value> std::ofstream& writeDat(std::ofstream& file, const T var) { return file.write(reinterpret_cast<char*>(&var), sizeof(var)); }
20th Apr 2018, 3:14 PM
Timon Paßlick
+ 1
Maybe you want something more general. You could overload a writeDat(var, file) and a readDat(var, file) function for any type where std::is_trivially_copyable is true with std::enable_if, for custom types and for any array or stl container of any supported type (including arrays and stl containers).
20th Apr 2018, 2:39 PM
Timon Paßlick
+ 1
Flash Your code or my code?
20th Apr 2018, 2:42 PM
Timon Paßlick
+ 1
Oh, I've written one some months ago but already deleted it because noone liked it.
20th Apr 2018, 2:50 PM
Timon Paßlick
+ 1
Let's try it again and help each other.
20th Apr 2018, 2:50 PM
Timon Paßlick
+ 1
It's a big task and I'm just saying that we could do it together on Skype/Discord/...
20th Apr 2018, 2:56 PM
Timon Paßlick
+ 1
That's the most thorough it can get. And I also gave you a general idea to start.
20th Apr 2018, 2:58 PM
Timon Paßlick
+ 1
#include <type_traits> #include <fstream> template <typename T> std::enable_if_t<std::is_trivially_copyable<T>::value> std::ofstream& writeDat(std::ofstream& file, const T& var) { return file.write(reinterpret_cast<char*>(&var), sizeof(var)); } template <typename Cont> ofstream& writeDat(ofstream& file, const Cont& cont) { writeDat(cont.size()); for (const auto& elem : cont) writeDat(elem); return file; }
20th Apr 2018, 3:42 PM
Timon Paßlick
+ 1
Nope. I didn't have the time. Were you able to fix the compiler errors without me?
20th Apr 2018, 8:05 PM
Timon Paßlick
0
Well, I need to learn the best and all the possible ways. Besides, the code is kinda ugly. But thanks for ur reply @Timon P
20th Apr 2018, 2:41 PM
Flash
0
Mine .. but could u post ur full code @Timon P
20th Apr 2018, 2:44 PM
Flash
0
@Timon P, well, ain't we programmers ;) .. I mean as a programmer we should be able to write codes whenever we r required to.. btw, it's okay.. thanks for ur contribution.. but I need a thorough answer.
20th Apr 2018, 2:54 PM
Flash
0
@Timon P Hey, thanks, but I don't have a skype or any other SNSs. But if u remember something do post it here. I was doing it for string vectors only, but storing any types would be a bonus. I liked ur ideas, will work on them. So, thanks for that.
20th Apr 2018, 3:01 PM
Flash
0
@Timon P, I tried to compile ur code in vs17, it didn’t compile. Did u test ur code?
20th Apr 2018, 6:09 PM
Flash