我一直在寻找,但我从来没有能够挽救其他比一个坐标中的任何一个2维数组你怎么一整个阵列保存到磁盘C++
-1
A
回答
1
你可以使用一个标准: :fstream或boost :: serialization。你的问题有点含糊,所以我不完全确定你想要什么,需要什么?
2
这叫做serialization。看看this question了解更多有关使用Boost做到这一点的信息。
0
如果数组包含平坦数据(即,不包括指向其它数据数据)时,可以写在单个写入到一个文件中的整个阵列。
不知道你的数据是什么样的,不可能多说这些。
0
一些修修补补后,我想出了这个:
#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;
}
改进建议,欢迎选购。
2
基于StackedCrooked's idea,这里有一个解决方案,它允许您使用C风格的std::vector
数组或其他元素为<<
定义的序列。
#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;
}
相关问题
- 1. 保存与列表列的磁盘到磁盘
- 2. BLOB保存到磁盘图像C#
- 3. 将PDF保存到本地磁盘C#
- 4. 列表保存到磁盘上
- 5. 将DynamicMethod保存到磁盘
- 6. Mongoengine FileField保存到磁盘?
- 7. 将NSMutableArray保存到磁盘
- 8. 将trie保存到磁盘
- 9. 将nsdate保存到磁盘
- 10. 磁盘阵列和字符串整数
- 11. 磁盘存储的阵列等
- 12. 写c型矩阵到磁盘
- 13. 转换磁盘阵列阵列
- 14. SK-学会保存模型到磁盘上,但只得到阵列
- 15. 磁盘阵列和阵列,内存问题
- 16. 保存Drawable到磁盘并检索 - Android
- 17. print.xtable只显示,不保存到磁盘
- 18. 将测试日志保存到磁盘?
- 19. 保存Pymc3模型到磁盘simmpling
- 20. 如何将ImageCache保存到磁盘中?
- 21. 将可变数据保存到磁盘
- 22. 保存大webgl纹理到磁盘
- 23. 脚本varbinary数据保存到磁盘
- 24. 将img/image的HTML保存到磁盘
- 25. 梨条码2 - 保存到磁盘
- 26. UWP将zip文件保存到磁盘
- 27. 将TransformedBitmap对象保存到磁盘。
- 28. 将Outlook附件保存到磁盘
- 29. 下载并保存文件到磁盘
- 30. Jasper Reports将PDF保存到磁盘
要说这是一个模糊的问题,会把它放得很温和。你使用的是什么类型的数组,C数组,像'std :: vector'这样的动态数组,还是别的?如果它是动态数组,你需要从保存的数据中找到大小,还是你知道它的前期?这些数组中存储了什么,您是否可以将这些类型的对象保存到磁盘? – sbi 2011-04-28 14:45:13