0

I am learning about C++ IO operations and have run into trouble reading from a binary file. I initialize an array with 100 random values, and write the values in the array to a .bin file. I think I got that part down -for some reason though my .bin file size is 1600 bytes instead of 400- but am struggling with reading the values.

I thought it was possible to read int by int by iterating through the memblock array containing the read values, but my console output shows a single random number followed by a bunch of zeroes. I would appreciate your guidance.

// reading a binary file int by int 
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main () {
    FILE * pFile; 
    streampos size;
    int * memblock;
    srand(time(NULL));
    int array[100];

    //data to write initially stored in array
    for (int i=0;i<100;i++){
        array[i]=rand()%100;
    }

    pFile = fopen("writebinary.bin","wb");
    fwrite(array,sizeof(int),sizeof(array),pFile);
    fclose(pFile);

    ifstream readFile("writebinary.bin",ios::binary | ios::ate);
    if (readFile.is_open()){
        size = readFile.tellg();
        //allocate memory to read file contents
        memblock = new int[size];
        readFile.seekg(0,ios::beg);
        for (int i=0;i<100;i++){ //100 because there are 100 ints,I'm assuming 32 bits are read a at a time from writebinary.bin
            readFile.read((char*)memblock,sizeof(int));
        }
        readFile.close();
    }
    else {
        cout << "can't open file " << endl;
    }
    for (int i=0;i<100;i++){
        cout << memblock[i] << endl;
    }
    return 0;
}
6
  • Have you tried writing and reading one int? Commented Jun 3, 2018 at 21:08
  • You shouldn't be use "ios::ate" when reading from the file. Commented Jun 3, 2018 at 21:13
  • yes, that is working fine Commented Jun 3, 2018 at 21:13
  • what's wrong with using ios::ate? Commented Jun 3, 2018 at 21:15
  • You are reading all 100 numbers into the first element of memblock. Commented Jun 3, 2018 at 21:17

1 Answer 1

2

Well, for your first issue, sizeof(array) is 400, because your array of 100 ints takes up 400 bytes, and sizeof(int) is 4, because an int is 32 bits. That's why you get a 1600 byte output file. You are hardcoding the number 100 everywhere else; you need to hardcode it here as well, or use sizeof(array)/sizeof(int).

Second, you don't need to iterate when using istream::read. It needs the size of the buffer to read, in bytes. So do readFile.read((char *)memblock,sizeof(array)); just once.

On another note, though, your code is a mish-mash of C-style programming and C++ iostreams programming. Why use fopen/fwrite/fclose? You should use std::ofstream and use the write member function, analogous to your readFile.read.

Sign up to request clarification or add additional context in comments.

3 Comments

Hardcoding 100 everywhere else is not entirely accurate: new int[tellg()] makes the same mistake yet again, allocating 1600 int objects (6400 bytes)
@BenVoigt Right, I was too fast there. Good catch.
yes, thanks. the reason I used a loop was because my assignment required it eg Perform file I/O in binary format with one value at a time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.