Upload files to "file"
This commit is contained in:
parent
911691b346
commit
43576bb55e
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
AUTORE: Manuel Vichi 3^AIN
|
||||||
|
Esercizio 1 File Binari
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdbool.h>
|
||||||
|
using namespace std;
|
||||||
|
int writeBinary(char * const fileName, size_t elements) {
|
||||||
|
FILE* file = fopen(fileName, "wb");
|
||||||
|
if (file == NULL) {
|
||||||
|
perror("Errore durante l'apertura del file di scrittura");
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
int arr[elements];
|
||||||
|
for (size_t i = 0; i < elements; i++) {
|
||||||
|
arr[i] = i;
|
||||||
|
}
|
||||||
|
int count = sizeof(arr) / sizeof(arr[0]);
|
||||||
|
int written = fwrite(&arr, sizeof(int), count,file);
|
||||||
|
fclose(file);
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int main(void) {
|
||||||
|
char file1[] = "file_70.bin";
|
||||||
|
unsigned elements1 = 70;
|
||||||
|
char file2[] = "file_100.bin";
|
||||||
|
unsigned elements2 = 100;
|
||||||
|
char file3[] = "file_200.bin";
|
||||||
|
unsigned elements3 = 200;
|
||||||
|
printf("Dimensione del tipo int: %d\n",sizeof(int));
|
||||||
|
printf("Elementi scritti con successo (file %s): %d\n",file1,writeBinary(file1,elements1));
|
||||||
|
printf("Elementi scritti con successo (file %s): %d\n",file2,writeBinary(file2,elements2));
|
||||||
|
printf("Elementi scritti con successo (file %s): %d\n",file3,writeBinary(file3,elements3));
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue