55 lines
863 B
C++
55 lines
863 B
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 09/05/2025
|
|
|
|
Creare un file di 10 byte casuali e contare quanti byte hanno il bit3 a 1
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
|
|
#define BYTE 10
|
|
|
|
using namespace std;
|
|
|
|
int main(void) {
|
|
srand(time(NULL));
|
|
|
|
FILE * file = fopen("file.bin", "wb");
|
|
|
|
if (file != NULL) {
|
|
for (int i = 0; i < BYTE; i++) {
|
|
char byte = rand() % 256;
|
|
fwrite(&byte, sizeof(char), 1, file);
|
|
}
|
|
|
|
fclose(file);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
|
|
file = fopen("file.bin", "rb");
|
|
|
|
if (file != NULL) {
|
|
int count = 0;
|
|
char byte;
|
|
|
|
for (int i = 0; i < BYTE; i++) {
|
|
fread(&byte, sizeof(char), 1, file);
|
|
|
|
if (byte & (1 << 3)) {
|
|
count++;
|
|
}
|
|
}
|
|
cout << count << endl;
|
|
|
|
fclose(file);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
|
|
return 0;
|
|
} |