如何将数组保存到C 中的文件中

How to save an array into a file in C++?

本文关键字:文件 保存 数组      更新时间:2023-10-16

我已经创建了一个程序,该程序将数组保存到文件中,然后读取文件以在屏幕上显示数组。这是代码

#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;