开发者

How do you save a whole array to a disk in c++

开发者 https://www.devze.com 2023-03-01 13:08 出处:网络
I\'ve been looking, but i\'ve never been able to save anything other than one coordinate in a 开发者_StackOverflow社区2 dimensional arrayThis is called serialization. Have a look at this question for

I've been looking, but i've never been able to save anything other than one coordinate in a 开发者_StackOverflow社区2 dimensional array


This is called serialization. Have a look at this question for more info on using Boost to do just that.


Based on StackedCrooked's idea, here's a solution that allows you to user either a C-style array of std::vector or any other sequence whose elements have << defined for them.

#include <cstddef>
#include <fstream>
#include <iostream>


// Beware, brain-compiled code ahead! 
template<class InpIt>
void save_seq(const std::ostream& os, InpIt begin, InpIt end)
{
    if(begin != end)
        os << *begin++;
    while(begin != end)
        os << ' ' << *begin++;
}

template<class OutpIt>
bool load_seq(const std::istream& is, OutpIt begin, std::size_t n)
{
    for( std::size_t i=0; is && i<n; ++i)
        is >> *begin++
    return is.good() || is.eof();
}
template<class OutpIt>
bool load_seq(const std::istream& is, OutpIt begin)
{
    while(is.good())
        is >> *begin++
    return is.eof();
}

template<class T, std::size_t N>
void save_array(const std::ostream& os, const T (&data)[N])
{
    save_seq(os, data, data+N);
}


template<class T, std::size_t N>
bool load_array(const std::istream& is, T (&data)[N])
{
    return load_seq(is, data, N);
}

int main()
{
    const std::size_t size = 5;
    int numbers[size];
    numbers[0] = 10;
    numbers[1] = 11;
    numbers[2] = 12;
    numbers[3] = 13;
    numbers[4] = 14;
    {
        std::oftsream ofs("array.txt");
        if(!ofs.good())
            return 1;
        save_array(ofs, numbers);
    }
    {
        std::iftsream ifs("array.txt");
        if(!ifs.good())
            return 2;
        int test[size];
        load_array(ifs, test);
        for (std::size_t idx = 0; idx < size; ++idx)
            std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
    }

    std::vector<int> numbers2;
    numbers2.push_back(20);
    numbers2.push_back(21);
    numbers2.push_back(22);
    numbers2.push_back(23);
    {
        std::oftsream ofs("array.txt");
        if(!ofs.good())
            return 1;
        save_Seq(ofs, numbers2.begin(), numbers2.end());
    }
    {
        std::iftsream ifs("array.txt");
        if(!ifs.good())
            return 2;
        std::vector<int> test;
        load_seq(ifs, std::back_inserter(test));
        for (std::size_t idx = 0; idx < numbers2.size(); ++idx)
            std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
    }

    return 0;
}


You could use a std::fstream or boost::serialization. Your question is a bit vague, so I'm not entirely sure what it is you want, need?


If the array contains flat data (i.e., data that does not include pointers to other data), you can write an entire array in a single write to a file.

Without any idea what your data looks like, it would not be possible to say much more about it.


After some tinkering I came up with this:

#include <cstddef>
#include <fstream>
#include <iostream>


template<class T>
void SaveArray(const std::string & file, T * data, std::size_t length)
{
    std::ofstream out(file.c_str());
    for (std::size_t idx = 0; idx < length; ++idx)
    {
        if (idx != 0)
        {
            out << " ";
        }
        out << *data++;
    }
}


template<class T>
std::size_t LoadArray(const std::string & file, T * data, std::size_t length)
{
    std::ifstream in(file.c_str());
    std::size_t count = 0;
    while (count++ < length && in >> *data++);
    return count - 1; // return number of items
}


int main()
{
    int numbers[5];
    numbers[0] = 10;
    numbers[1] = 11;
    numbers[2] = 12;
    numbers[3] = 13;
    numbers[4] = 14;
    SaveArray("array.txt", &numbers[0], 5);

    int test[5];
    LoadArray("array.txt", &test[0], 5);

    for (std::size_t idx = 0; idx < 5; ++idx)
    {
        std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
    }
    return 0;
}

Suggestions for improvement are welcome.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号