簡體   English   中英

如何在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。

在第二行中,您打算讀取 5 個整數,因此應將其更改為

int load[sizeLoad];
fileRead.read((char*)&load, sizeLoad * sizeof(int));

您還需要更改循環,只需從0sizeLoad

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM