如何将数组保存到C 中的文件中
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个整数,因此应将其更改为
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;
编辑:我尝试解释不清楚的部分。在创建" .dat"文件的3.line中,下一行您将数组中的每个数字都放在该文件中。 ofstream.write 函数采用两个参数,第一个参数应该是字符,因此我将数组投入到char,第二个参数应该是数组的大小(您将编写多少个字符)。在您的情况下,您的数组包含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;
相关文章:
- 无法通过头文件保存变量的值
- 是否有标准的文件保存和交换模式
- C++两次从文件保存对象读取多重继承
- 如何选择文件保存到的位置?
- 如何将文本文件保存到C 中的字符串结构
- 将文件保存到目录C
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 从文件C++保存和读取双向量
- 使用 depend将对象文件保存在相同的生成文件目录中
- 在C++中创建文件时,创建的文件保存在何处
- 在 cab 应用程序卸载时将文件保存在文件夹中
- 读取.txt文件保存在内存中
- 如何将CSV文件保存为UTF-8
- 使用iostream将文件保存在路径
- 在Qt中将文件保存到网络
- 将链表写入 txt 文件(保存过程)
- 在 C++ 中将编辑过的内文件保存到输出文件时出现问题
- 将文件保存在不同的linux位置
- 多线程文件保存操作
- 文件保存期间的进度