Upload files to "file"

This commit is contained in:
Vichingo455 2025-05-10 06:59:25 +00:00
parent 8859e81c7a
commit fab6d48bab
1 changed files with 51 additions and 0 deletions

51
file/es6_binary.cpp Normal file
View File

@ -0,0 +1,51 @@
/*
AUTORE: Manuel Vichi 3^AIN
Esercizio 6 File Binari
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int countBitOne(const char string[], const unsigned bit, const size_t lenght) {
unsigned count = 0;
for (size_t i = 0; i < lenght; i++) {
if ((string[i] >> bit) & 1) {
count++;
}
}
return count;
}
void generate(char string[], const size_t size) {
srand(time(NULL));
for (size_t i = 0; i < size; i++) {
string[i] = (rand() % 26) + 'A';
}
}
int writeBinary(const char * fileName, const char arr[], const size_t count) {
FILE* file = fopen(fileName,"wb");
if (file != NULL) {
int written = fwrite(arr,sizeof(char),count,file);
fclose(file);
return written;
} else {
perror("Errore durante l'apertura del file di scrittura");
return -1;
}
return -1;
}
int main(void) {
char fileName[] = "girmilloblu.bin";
char arr[10];
unsigned bitNumber = 3;
generate(arr,sizeof(arr));
printf("Array generato: ");
for (unsigned i = 0; i < sizeof(arr); i++) {
printf("%c", arr[i]);
}
printf("\n");
size_t count = sizeof(arr) / sizeof(arr[0]);
printf("Valori scritti nel file %s: %d\n", fileName, writeBinary(fileName,arr,count));
printf("Byte con il bit %d a 1: %d", bitNumber, countBitOne(arr,bitNumber,sizeof(arr)));
return 0;
}