56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 13/05/2025
|
|
|
|
Converti un file di testo in un file binario e viceversa:
|
|
1) Numeri interi (uno per ogni riga nel file di testo);
|
|
2) Numeri float (uno per ogni riga nel file edi testo);
|
|
3) Stringhe lunghe 20 (una per ogni riga nel file di testo);
|
|
4) Struct:
|
|
- Numeri complessi (uno per ogni riga nel file di testo);
|
|
- Libro: titolo, prezzo, nPagine.
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
typedef struct {
|
|
char titolo[20+1];
|
|
float prezzo;
|
|
int nPagine;
|
|
} Libro;
|
|
|
|
int main(void) {
|
|
const char * fileNameIn = "libri.bin";
|
|
const char * fileNameOut = "libri.txt";
|
|
|
|
FILE * fileIn = fopen(fileNameIn, "rt");
|
|
|
|
if (fileIn != NULL) {
|
|
FILE * fileOut = fopen(fileNameOut, "wb");
|
|
|
|
if (fileOut != NULL) {
|
|
Libro libro;
|
|
|
|
while (fread(&libro, sizeof(Libro), 1, fileIn) == 1) {
|
|
fprintf(fileOut, "%s\t%f\t%d", libro.titolo, libro.prezzo, libro.nPagine);
|
|
cout << "Titolo: " << libro.titolo << endl <<
|
|
"Prezzo: " << libro.prezzo << endl <<
|
|
"Numero di pagine: " << libro.nPagine << endl << endl;
|
|
}
|
|
|
|
fclose(fileOut);
|
|
} else {
|
|
perror("Error (destination)");
|
|
}
|
|
|
|
fclose(fileIn);
|
|
} else {
|
|
perror("Error (source)");
|
|
}
|
|
|
|
return 0;
|
|
} |