[英]How to save an array into a file in C++?
我创建了一个程序,将数组保存到文件中,然后读取文件以在屏幕上显示数组。 这是代码
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
int saveSize = 5;//size of the array
int save [saveSize + 1] = {saveSize, 7, 1, 2 ,3, 4}; //creating an array which first element is his own size
ofstream fileWrite; fileWrite.open ("File.dat", ios::out | ios::binary | ios::trunc);
fileWrite.write((char *) &save, sizeof(save)); //saves the array into the file
fileWrite.close();
int sizeLoad; //size of the array that will be loaded
ifstream fileRead; fileRead.open("File.dat", ios::in | ios::binary);
fileRead.read((char *) &sizeLoad, sizeof(int)); //it only reads the first element to know the size of the array
int load[sizeLoad+1]; //creating the array
fileRead.read((char *) &load, sizeof(int));
fileRead.close();
//showing the array
for(int i = 1; i <= sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
return 0;
}
问题是当我运行程序时,结果是:
元素 1:2686224
要素2:1878005308
元素 3:32
元素 4:2686232
元素 5:4199985
差远了。 有人知道为什么会显示吗?
fileRead.read((char *) &sizeLoad, sizeof(int))
fileRead.read((char*)&load, sizeof(int));
你读到sizeLoad
是 5。
在第二行中,您打算读取 5 个整数,因此应将其更改为
int load[sizeLoad];
fileRead.read((char*)&load, sizeLoad * sizeof(int));
您还需要更改循环,只需从0
到sizeLoad
for(int i = 0; i < sizeLoad; i++)
cout << "Element " << i << ": " << load[i] << endl;
或者,您可以使用std::vector
,那么您不必提前保存项目数。 您可以一次读取一个整数并使用std::push_back
向数组添加元素。 例子:
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::vector<int> save = { 7, 1, 2, 3, 4 };
std::ofstream fileWrite("File.dat", std::ios::binary | std::ios::trunc);
fileWrite.write((char*)save.data(), save.size() * sizeof(int));
fileWrite.close();
std::ifstream fileRead("File.dat", std::ios::binary);
std::vector<int> load;
int temp;
while(fileRead.read((char*)&temp, sizeof(int)))
load.push_back(temp);
fileRead.close();
for(int i = 0; i < load.size(); i++)
std::cout << "Element " << i << ": " << load[i] << std::endl;
return 0;
}
我改变了你的代码,它有效。 尝试以下;
int saveSize = 5; //size of the array
int save[saveSize + 1] = {saveSize, 7, 1, 2 ,3, 4}; //creating an array which first element is his own size
ofstream fileWrite("File.dat", ios::out | ios::binary | ios::trunc);
fileWrite.write(reinterpret_cast<char*>(&save), sizeof save ); //saves the array into the file
fileWrite.close();
int sizeLoad[saveSize+1]; //size of the array that will be loaded
ifstream fileRead("File.dat", ios::in | ios::binary);
fileRead.read(reinterpret_cast<char*>(&sizeLoad), sizeof sizeLoad ); //it only reads the first element to know the size of the array
fileRead.close();
//showing the array
for(int i = 0; i < (sizeof(sizeLoad)/sizeof(sizeLoad[0])); i++)
cout << "Element " << i + 1 << ": " << sizeLoad[i] << endl;
return 0;
编辑:我尝试解释不清楚的部分。 在 3.line 中,您创建了“.dat”文件,下一行将数组中的每个数字都放入该文件中。 ofstream.write函数需要两个参数,第一个参数应该是字符,所以我将数组转换为字符,第二个参数应该是数组的大小(您将写入多少个字符)。 在您的情况下,您的数组包含 6 个数字,数组大小为 24(每个 int 值 4 字节)。 在将数组写入控制台时,您需要知道数组的大小,因此我只需将数组大小 (24) 除以 1 个数组字符 (4)。 对不起,我的错误解释并感谢您的谨慎。
你已经阅读了一个元素,
fileRead.read((char *) &load, sizeof(int)); // load[0] was read
没有打印
for(int i = 1; i <= sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
您应该使用读取完整数组
int load[sizeLoad]; //creating the array
fileRead.read((char *) &load, sizeof(load));
并以这种方式打印所有元素
for(int i = 0; i < sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.