1

I've serialised a map fine

std::map<std::string, std::string> userandPass; 
saveData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass); // have removed &. why is this needed. i want to pass by reference

using the function

template <typename SaveClass>
void saveData(const std::string filename, SaveClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() / 
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() / 
filename;
// Declare an output file stream ofs that writes serialises to our 
//backup file.
boost::filesystem::ofstream ofs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_oarchive ta(ofs);
// Write to file
ta << c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed 
up to " << myFile << "\n";
}

Deserialising (loading) however, fails, using:

loadData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass);

where the function is:

template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() / 
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() / 
filename;
// Declare an output file stream ofs that writes serialises to our 
//backup file.
boost::filesystem::ifstream ifs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_iarchive ta(ifs);
// Write to file
ta >> c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed 
up to " << myFile << "\n";
}

My project compiles, but when I run it, I get the following error concerning the input stream:

libc++abi.dylib: terminating with uncaught exception of type boost::archive::archive_exception: input stream error Abort trap: 6

I don't see why this is happening. Would anyone be so kind as to point me in the right direction?

Thanks

0

2 Answers 2

2

It seems like you copypasted loadData body from saveData. You delete file that you are trying to load as a first step by calling boost::filesystem::remove.

Sign up to request clarification or add additional context in comments.

1 Comment

Of course I did. Silly mistake! Thanks a million for your help. Have a great day!
0

@VTT got the biggest bug.

Here's my free code review:

  • you don't need boost::filesystem to std::remove(filename)
  • instead of currentdir / filename you should do boost::filesystem::make_absolute
  • that is already default behaviour
  • you should not use native() since you can pass the path
  • the argument to saveData can be const&
  • Do not explicitly pass template arguments: function calls do template argument deduction.
  • Never mind that your comments are very redundant.

Live On Coliru

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <cstdio>
#include <iostream>
#include <fstream>

template <typename SaveClass>
void saveData(const std::string filename, SaveClass const& c)
{
    std::remove(filename.c_str());
    std::ofstream ofs(filename);
    boost::archive::text_oarchive ta(ofs);

    ta << c;
}

template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
    std::ifstream ifs(filename);
    boost::archive::text_iarchive ta(ifs);
    ta >> c;
}

int main() {
    std::string filename = "userandPassBackup.txt";
    {
        std::map<std::string, std::string> userandPass {
            { "John", "pa$$w0rd" },
            { "Jane", "welcome01" } };
        saveData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully backed up to " << filename << "\n";
    }
    {
        std::map<std::string, std::string> userandPass;
        loadData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully restored from " << filename << "\n";
    }
}

Prints

2 records from have been successfully backed up to userandPassBackup.txt
2 records from have been successfully restored from userandPassBackup.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.