I've created a program that saves an array into a file and then reads the file to show the array on the screen. This is the code
#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;
}
The problem is that when I run the program, that´s the result:
Element 1: 2686224
Element 2: 1878005308
Element 3: 32
Element 4: 2686232
Element 5: 4199985
Not even close. Somebody knows why it shows that?
fileRead.read((char *) &sizeLoad, sizeof(int))
fileRead.read((char*)&load, sizeof(int));
You read sizeLoad
which is 5.
In the second line you intend to read 5 integers, therefore it should be changed to
int load[sizeLoad];
fileRead.read((char*)&load, sizeLoad * sizeof(int));
You also need to change the loop, only go from 0
to sizeLoad
for(int i = 0; i < sizeLoad; i++)
cout << "Element " << i << ": " << load[i] << endl;
Alternatively you can use std::vector
, then you don't have to save the number of items in advance. You can read integers one at a time and use std::push_back
to add elements to the array. Example:
#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;
}
I changed your code, and it works. Try following;
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;
Edit : I try to explain unclear parts. In the 3.line you created ".dat" file and next line you put every number in the array to that file. ofstream.write function takes two parameters, the first parameters should be characters so I cast array to char, second parameter should be size of array (How many characters you will write). In your case your array contains 6 numbers and your array size is 24 (Each int value 4 byte). While writing array to console, you need to know your size of array so I simply divide the array size (24) to 1 array character (4). Sorry for my bad explanation and thanks for caution.
You have read one element,
fileRead.read((char *) &load, sizeof(int)); // load[0] was read
which was not printed
for(int i = 1; i <= sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
You should read full array using
int load[sizeLoad]; //creating the array
fileRead.read((char *) &load, sizeof(load));
and print all elements in this way
for(int i = 0; i < sizeLoad; i++) cout << "Element " << i << ": " << load[i] << endl;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.