如何将数组保存到C++中的文件中?
我创建了一个程序,将数组保存到一个文件中,然后读取该文件以在屏幕上显示数组。这是密码如何将数组保存到C++中的文件中?,c++,arrays,file,C++,Arrays,File,我创建了一个程序,将数组保存到一个文件中,然后读取该文件以在屏幕上显示数组。这是密码 #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 arra
#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 *) &load, sizeof(int)); // load[0] was read
没有打印出来
for(int i = 1; i <= sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
并以这种方式打印所有元素
for(int i = 0; i < sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
我更改了你的代码,它工作正常。试着跟随
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;
编辑:我试图解释不清楚的部分。在创建的.dat文件的3.1行中,下一行将数组中的每个数字都放入该文件中。
write函数接受两个参数,第一个参数应该是字符,所以我将数组转换为字符,第二个参数应该是数组的大小,您将写入多少字符。在本例中,数组包含6个数字,数组大小为24,每个int值为4字节。在将数组写入控制台时,您需要知道数组的大小,所以我只需将数组大小24除以1个数组字符4。对不起,我的解释不好,谢谢你的提醒
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
比如int-load[sizeLoad+1];不是有效的C++。不要使用数组,请使用std::vector。强烈建议读取:!您可能应该只使用写入和读取文件,而不是将数组视为char*——这样更简单,更不容易出错;fileWrite.open。。。;可以更简单地写入流文件写入。。。;。这就是构造函数的作用。此外,ofstream的析构函数会关闭文件,因此无需调用fileWrite.close。每一个带有标签C++的第三个代码使用这个页面上的VLA,但是只有我得到了投票。这看起来是正确的。不,它看起来不正确,不是C++。因此,是下票。每第三个带有标签C++的代码在这个页面上使用VLA,但只有我投票失败。废话。如果你想限制你的可移植性,公爵允许你使用它们,但是这个问题被标记为C++,而不是GCC。我真的不理解你的代码中的几行,但我会尝试找出它们是什么。这不是一个完整的答案。你需要解释你改变了什么以及为什么你改变了它。也不能正确地复制askers代码的意图。它可以编译和运行,但是做了一些稍微不同的事情。哇,这正是我想要的,而且更简单。。。谢谢D
int load[sizeLoad];
fileRead.read((char*)&load, sizeLoad * sizeof(int));
for(int i = 0; i < sizeLoad; i++)
cout << "Element " << i << ": " << load[i] << endl;
#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;
}