School-Coding-Cpp/sfusi/bin2txt.cpp

39 lines
823 B
C++

#include <iostream>
using namespace std;
typedef struct {
char cognome[100+1];
char nome[100+1];
int anno;
} Structure;
int main(void) {
const char * fileNameIn = "fileIn.bin";
const char * fileNameOut = "fileOut.txt";
FILE * fileIn = fopen(fileNameIn, "rb");
if (fileIn != NULL) {
FILE * fileOut = fopen(fileNameOut, "wt");
if (fileOut != NULL) {
Structure structure;
while (fread(&structure, sizeof(structure), 1, fileIn) == 1) {
fprintf(fileOut, "%s\t%s\t%d", structure.cognome, structure.nome, structure.anno);
cout << structure.cognome << " " << structure.nome << " " << structure.anno;
}
fclose(fileOut);
} else {
perror("Error (destination)");
}
fclose(fileIn);
} else {
perror("Error (source)");
}
return 0;
}