handle fread/fwrite errors more gracefully
This commit is contained in:
parent
fdce5a50b8
commit
4067da453e
2 changed files with 12 additions and 0 deletions
4
exhal.c
4
exhal.c
|
@ -58,6 +58,10 @@ int main (int argc, char **argv) {
|
|||
// write the uncompressed data to the file
|
||||
fseek(outfile, 0, SEEK_SET);
|
||||
fwrite((const void*)unpacked, 1, outputsize, outfile);
|
||||
if (ferror(outfile)) {
|
||||
perror("Error writing output file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("Uncompressed size: %zu bytes\n", outputsize);
|
||||
} else {
|
||||
|
|
8
inhal.c
8
inhal.c
|
@ -89,6 +89,10 @@ int main (int argc, char **argv) {
|
|||
// read the file
|
||||
fseek(infile, 0, SEEK_SET);
|
||||
fread(unpacked, sizeof(uint8_t), inputsize, infile);
|
||||
if (ferror(infile)) {
|
||||
perror("Error reading input file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// compress the file
|
||||
clock_t time = clock();
|
||||
|
@ -99,6 +103,10 @@ int main (int argc, char **argv) {
|
|||
// write the compressed data to the file
|
||||
fseek(outfile, fileoffset, SEEK_SET);
|
||||
fwrite((const void*)packed, 1, outputsize, outfile);
|
||||
if (ferror(outfile)) {
|
||||
perror("Error writing output file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("Compressed size: % zu bytes\n", outputsize);
|
||||
printf("Compression ratio: %4.2f%%\n", 100 * (double)outputsize / inputsize);
|
||||
|
|
Loading…
Reference in a new issue