1

I need to write/read a file that contains a std::map. The file must be read at the program start up (if it exists). Im using boost's fstream, but im getting this:

"terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  input stream error"

Well, i really dont know what is happening.. these are my lines:

map<int64_t, int64_t> foo;
filesystem::path myFile = GetWorkingDirectory() / "myfile.dat";
[...............] // some code

filesystem::ifstream ifs(myFile);
archive::text_archive ta(ifs);
if (filesystem::exists(myFile)
{
   ta >> foo; // foo is empty until now, it's fed by myFile
   ifs.close();
}

What im doing wrong? Any idea? Thanks.

P.S. Note that some lines after, i need to do the reverse action: write into myfile.dat the std::map foo.

EDIT: all works if i use std::ifstream, saving the file in the same dir where im running the Application. But using boost and his path, something goes wrong.

sehe
  • 374,641
  • 47
  • 450
  • 633
sciakysystem
  • 97
  • 1
  • 2
  • 10
  • What's your actual input? – πάντα ῥεῖ Feb 21 '15 at 21:01
  • i tried with no input (deleting the file) and with a file.dat which i previously created in the same way with std::ofstream..and both gave me the error above – sciakysystem Feb 21 '15 at 21:03
  • how could a file contain a `std::map`? – max Feb 21 '15 at 21:21
  • http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/index.html afaik – sciakysystem Feb 21 '15 at 21:23
  • where in the link does it talk about `std::map` in a file? I can't see it. – max Feb 21 '15 at 21:26
  • @Barracuda see my answer. Also, there's memory maps with custom allocators, which make this even more direct (see e.g. http://stackoverflow.com/questions/26342351/using-stl-containers-for-boostinterprocessmanaged-shared-memory/26347924#26347924) – sehe Feb 21 '15 at 21:28
  • @sehe I've never seen anyone do this using standard library. Is this possible in C++11 or C++14? All I can find online is specific to boost. – max Feb 21 '15 at 21:33
  • Well the question is specific to boost too @Barracuda. And yes you can do this with C++11 using stateful allocators and a POSIX `mmap` call – sehe Feb 21 '15 at 21:33
  • @sehe I'm not blaming the OP. It's actually a new idea to me. I don't even know if I'm allowed to post a comment like this. Sorry but I'm kinda surprised. I mean this is awesome and I didn't downvote the question just to clear things up. – max Feb 21 '15 at 21:34

1 Answers1

3

I'm a bit miffed. You're clearly using Boost Serialization (the archive/ headers are part of this library) but somehow you're not saying anything about that. Because it's so easy to demonstrate:

Live On Coliru

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

using namespace boost;

int main() {
    std::map<int64_t, int64_t> foo;
    filesystem::path myFile = filesystem::current_path() / "myfile.dat";

    if (filesystem::exists(myFile))
    {
        filesystem::ifstream ifs(myFile/*.native()*/);
        archive::text_iarchive ta(ifs);

        ta >> foo; // foo is empty until now, it's fed by myFile

        std::cout << "Read " << foo.size() << " entries from " << myFile << "\n";
    } else {
        for (int i=0; i<100; ++i) foo.emplace(rand(), rand());
        filesystem::ofstream ofs(myFile/*.native()*/);
        archive::text_oarchive ta(ofs);

        ta << foo; // foo is empty until now, it's fed by myFile
        std::cout << "Wrote " << foo.size() << " random entries to " << myFile << "\n";
    }

}

Prints

Wrote 100 random entries to "/tmp/myfile.dat"

And on second run:

Read 100 entries from "/tmp/myfile.dat"
sehe
  • 374,641
  • 47
  • 450
  • 633
  • first of all thanks for your prompt reply. Well, sorry for my not-well-written question but you hit the point anyway. My code is exactly as your example.. so why i got that error? im so confused.. – sciakysystem Feb 22 '15 at 00:55
  • EDIT: i solved changing the path.. not sure of why, but now it works properly. Thanks however, – sciakysystem Feb 22 '15 at 01:24