如何在C ++中将数组保存到文件中?

时间:2017-12-27 18:16:57

标签: 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 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

甚至没有关闭。有人知道它为什么表明这个?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

我改变了你的代码,它确实有效。请尝试以下内容;

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 函数有两个参数,第一个参数应该是字符,所以我将数组转换为char,第二个参数应该是数组的大小(你要编写多少个字符)。在您的情况下,您的数组包含6个数字,您的数组大小为24(每个int值为4个字节)。在将数组写入控制台时,您需要知道数组的大小,因此我只需将数组大小(24)除以1个数组字符(4)。对不起我的不好解释并感谢您的谨慎。

答案 2 :(得分:-2)

您已阅读一个元素

const { Client } = require('pg');
const client = new Client();

async connectClient (client) { 
   await client.connect();
   return client;
}

async disconnectClient(client) {
  await client.end()
}

未打印

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