-2

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?

DeiDei
  • 10,205
  • 6
  • 55
  • 80
Cnoob
  • 169
  • 1
  • 3
  • 10
  • 6
    Things like `int load[sizeLoad+1];` are not valid C++. Don't use an array, use a std::vector. –  Dec 27 '17 at 18:19
  • Highly recommended read: https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr ! – user0042 Dec 27 '17 at 18:31
  • You should probably just use `<<` and `>>` to write to and read from file instead of treating the array as a `char*` - it's much simpler and less prone to error. – Bernhard Barker Dec 27 '17 at 18:35
  • Note that `ofstream fileWrite; fileWrite.open (...);` can be written more simply as `ofstream fileWrite(...);`. That's what the constructor is for. Also, the destructor for `ofstream` closes the file, so calling `fileWrite.close()` is not necessary. Same for `fileRead`. – Pete Becker Dec 27 '17 at 18:39

3 Answers3

1
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;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
-1

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.

yenicead
  • 15
  • 5
  • Oh thanks! I don´t really understand a few lines of your code but I will try to figure out wat they do. – Cnoob Dec 27 '17 at 19:14
  • 1
    This is not a complete answer. You need to explain what you changed and why you changed it. Also doesn't replicate the intent of the askers code correctly. It compiles and runs, but does something slightly different. – user4581301 Dec 27 '17 at 19:14
-2

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;
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • Every third code with tag c++ uses VLA on this page, but only I got downvote. – rafix07 Dec 27 '17 at 18:50
  • "This appears to be correct" - no, it appears to be incorrect (it's not C++) - hence the downvotes. –  Dec 27 '17 at 18:52
  • "Every third code with tag c++ uses VLA on this page, but only I got downvote. " - nonsense. –  Dec 27 '17 at 18:54
  • @Duke You are "allowed" to use them if you want to limit your portability, but this question was tagged C++, not GCC. –  Dec 27 '17 at 18:59