Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数组保存到C++;_C++ - Fatal编程技术网

C++ 将数组保存到C++;

C++ 将数组保存到C++;,c++,C++,我有一个名为Array的自定义类。它存储类型为T的数组和一个大小整数 将数组保存到文件中是否正确 fout.write((char *)m_array, sizeof(T) * m_size); 我如何存储它: bool save(const std::string &filename) { std::ofstream fout; // Open the file. fout.open(filename, std::io

我有一个名为Array的自定义类。它存储类型为T的数组和一个大小整数

将数组保存到文件中是否正确

fout.write((char *)m_array, sizeof(T) * m_size);
我如何存储它:

    bool save(const std::string &filename)
    {
        std::ofstream fout;

        // Open the file.
        fout.open(filename, std::ios::out | std::ios::binary);

        // Validate that the file is open.
        if (!fout.is_open())
            return false;

        // Write the size to file.
        fout << m_size << std::endl;  

        fout.write((char *)m_array, sizeof(T) * m_size);

        fout.close();

        return true;
    }
主文件:

Array<int> a(10);

a.clear(); // set all values to 0. 

a[5] = 3; 

if (a.save("test.dat"))
    con.writeLine("Saved");
我得到10,0,0,0768,0,0,0,0,0

我做错了什么?第二个参数表示它需要计数,对我来说是sizeof(T)*m_size

更新: 另一种选择是在保存时:

for (int i = 0; i < m_size; i++)
fout << m_array[i] << std::endl;
for(int i=0;ifout您将格式化和未格式化写入混合到流中。而不是:

fout << m_size << std::endl;
但有了这一点:

fin >> size;
fin.read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
给定
ifstream-fin
,您可以这样阅读:

const auto a_size = size(a);

fout.write(reinterpret_cast<const char*>(&a_size), sizeof(a_size));
fout.write(reinterpret_cast<const char*>(data(a)), sizeof(decltype(a)::value_type) * a_size);
size_t a_size;

fin.read(reinterpret_cast<char*>(&a_size), sizeof(a_size));
a.resize(a_size);
fin.read(reinterpret_cast<char*>(data(a)), sizeof(decltype(a)::value_type) * a_size);
size\u\t a\u size;
fin.read(重新解释铸件和a_尺寸)、尺寸(a_尺寸));
a、 调整大小(a_大小);
fin.read(重新解释数据(a)),sizeof(数据类型(a)::值类型)*大小;

您将格式化和未格式化写入混合到流中。而不是:

fout << m_size << std::endl;
但有了这一点:

fin >> size;
fin.read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
给定
ifstream-fin
,您可以这样阅读:

const auto a_size = size(a);

fout.write(reinterpret_cast<const char*>(&a_size), sizeof(a_size));
fout.write(reinterpret_cast<const char*>(data(a)), sizeof(decltype(a)::value_type) * a_size);
size_t a_size;

fin.read(reinterpret_cast<char*>(&a_size), sizeof(a_size));
a.resize(a_size);
fin.read(reinterpret_cast<char*>(data(a)), sizeof(decltype(a)::value_type) * a_size);
size\u\t a\u size;
fin.read(重新解释铸件和a_尺寸)、尺寸(a_尺寸));
a、 调整大小(a_大小);
fin.read(重新解释数据(a)),sizeof(数据类型(a)::值类型)*大小;

添加
fin.get()
fin>>大小之后添加
fin.get()
fin>>大小之后
。使用此项检索时,我得到了十个-84215051。在读取大小时,我是否需要进行相同的重新解释?在读取和写入时添加此选项将得到正确的结果。您不能直接读取到
m\u size
。如果
数组的大小不同,则会发生错误,因为您不知道是否需要调整其大小。您仍然需要读入临时的
大小
。是的,我有这个,并且海报可以选择包含临时本地大小,而不是使用成员变量。谢谢你的回答,@JonathanMee@user4581301,只要他读入它,然后立即用它来调整容器大小就可以了。使用这个,我得到了10-84215051的检索结果。在读取大小时,我是否需要进行相同的重新解释?在读取和写入时添加此选项将得到正确的结果。您不能直接读取到
m\u size
。如果
数组的大小不同,则会发生错误,因为您不知道是否需要调整其大小。您仍然需要读入临时的
大小
。是的,我有这个,并且海报可以选择包含临时本地大小,而不是使用成员变量。谢谢你的回答,@JonathanMee@user4581301,只要他读入它,然后立即使用它调整容器大小就可以了。