/*
AUTORE: Manuel Vichi 3^AIN
Legge da un file binario e memorizza in un array.
*/
#include <iostream>
using namespace std;
size_t readValues(FILE* fptr, int storage[]) {
	rewind(fptr);
	size_t size = sizeof(storage) / sizeof(storage[0]);
	for (size_t i = 0; i < size; i++) {
		storage[i] = 0;
	}
	return fread(&storage,sizeof(int),size,fptr);
}
size_t readValuesLoop(FILE* fptr, int storage[]) {
	rewind(fptr);
	int value = 0;
	size_t size = sizeof(storage) / sizeof(storage[0]);
	for (size_t i = 0; i < size; i++) {
		storage[i] = 0;
	}
	size_t counter = 0;
    while (fread(&value, sizeof(int), 1, fptr) == 1 && counter < sizeof(storage)) {
        storage[counter] = value;
        counter++;
    }
    return counter;
}
int main(void) {
	FILE* file = fopen("file_70.bin","rb");
	if (file != NULL) {
		int arr[100];
		int arr2[100];
		cout << "Valori letti (singola fread): " << readValues(file,arr) << endl;
		cout << "Valori letti (ciclo while): " << readValuesLoop(file,arr2) << endl;
		cout << "Array 1: ";
		size_t size = sizeof(arr) / sizeof(arr[0]);
		for (size_t i = 0; i < size; i++) {
			cout << arr[i] << " ";
		}
		cout << endl << "Array 2: ";
		size = sizeof(arr2) / sizeof(arr2[0]);
		for (size_t i = 0; i < sizeof(arr2); i++) {
			cout << arr2[i] << " ";
		}
		cout << endl;
		fclose(file);
	}
	return 0;
}
