Loading lang_c_ex_0803...
typedef struct { int hour; int minute; int second; double subsecond; } Duration;
void test_Duration(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
typedef struct { double price; double weight; int shipping; } Article;
void test_Article(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
typedef struct { double delta; int count; double x1; double x2; } Solution;
void test_Solution(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
//----------------------------------------------------------------------------
#ifndef BINARYINOUT_H #define BINARYINOUT_H
#include <stdbool.h>
typedef struct { int hour; int minute; int second; double subsecond; } Duration;
bool // success Duration_saveBin(const Duration *duration, const char *fileName);
bool // success Duration_loadBin(Duration *out_duration, const char *fileName);
typedef struct { double price; double weight; int shipping; } Article;
bool // success Article_saveBin(const Article *article, const char *fileName);
bool // success Article_loadBin(Article *out_article, const char *fileName);
typedef struct { double delta; int count; double x1; double x2; } Solution;
bool // success Solution_saveBin(const Solution *solution, const char *fileName);
bool // success Solution_loadBin(Solution *out_solution, const char *fileName);
#endif // BINARYINOUT_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "binaryInOut.h"
#include <stdio.h>
bool // success Duration_saveBin(const Duration *duration, const char *fileName) { FILE *output=fopen(fileName, "wb"); // binary mode if(output==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler fwrite(duration, sizeof(Duration), 1, output); #else fwrite(&duration->hour, sizeof(int), 1, output); fwrite(&duration->minute, sizeof(int), 1, output); fwrite(&duration->second, sizeof(int), 1, output); fwrite(&duration->subsecond, sizeof(double), 1, output); #endif fclose(output); return true; }
bool // success Duration_loadBin(Duration *out_duration, const char *fileName) { FILE *input=fopen(fileName, "rb"); // binary mode if(input==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler const bool success=fread(out_duration, sizeof(Duration), 1, input)==1; #else const bool success= (fread(&out_duration->hour, sizeof(int), 1, input)==1)&& (fread(&out_duration->minute, sizeof(int), 1, input)==1)&& (fread(&out_duration->second, sizeof(int), 1, input)==1)&& (fread(&out_duration->subsecond, sizeof(double), 1, input)==1); #endif fclose(input); return success; }
bool // success Article_saveBin(const Article *article, const char *fileName) { FILE *output=fopen(fileName, "wb"); // binary mode if(output==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler fwrite(article, sizeof(Article), 1, output); #else fwrite(&article->price, sizeof(double), 1, output); fwrite(&article->weight, sizeof(double), 1, output); fwrite(&article->shipping, sizeof(int), 1, output); #endif fclose(output); return true; }
bool // success Article_loadBin(Article *out_article, const char *fileName) { FILE *input=fopen(fileName, "rb"); // binary mode if(input==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler const bool success=fread(out_article, sizeof(Article), 1, input)==1; #else const bool success= (fread(&out_article->price, sizeof(double), 1, input)==1)&& (fread(&out_article->weight, sizeof(double), 1, input)==1)&& (fread(&out_article->shipping, sizeof(int), 1, input)==1); #endif fclose(input); return success; }
bool // success Solution_saveBin(const Solution *solution, const char *fileName) { FILE *output=fopen(fileName, "wb"); // binary mode if(output==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler fwrite(solution, sizeof(Solution), 1, output); #else fwrite(&solution->delta, sizeof(double), 1, output); fwrite(&solution->count, sizeof(int), 1, output); fwrite(&solution->x1, sizeof(double), 1, output); fwrite(&solution->x2, sizeof(double), 1, output); #endif fclose(output); return true; }
bool // success Solution_loadBin(Solution *out_solution, const char *fileName) { FILE *input=fopen(fileName, "rb"); // binary mode if(input==NULL) { return false; } #if 0 // very specific to the padding chosen by the compiler const bool success= fread(out_solution, sizeof(Solution), 1, input)==1; #else const bool success= (fread(&out_solution->delta, sizeof(double), 1, input)==1)&& (fread(&out_solution->count, sizeof(int), 1, input)==1)&& (fread(&out_solution->x1, sizeof(double), 1, input)==1)&& (fread(&out_solution->x2, sizeof(double), 1, input)==1); #endif fclose(input); return success; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "binaryInOut.h"
#include <stdio.h>
void test_Duration(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Duration d1={1, 5, 17, 0.854}; printf("saving duration to binary file: %d %d %d %g\n", d1.hour, d1.minute, d1.second, d1.subsecond); if(!Duration_saveBin(&d1, "output_data")) { fprintf(stderr, "cannot save duration to binary file\n"); } else { Duration d2; if(!Duration_loadBin(&d2, "output_data")) { fprintf(stderr, "cannot load duration from binary file\n"); } printf("loaded duration from binary file: %d %d %d %g\n", d2.hour, d2.minute, d2.second, d2.subsecond); } }
void test_Article(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Article a1={49.99, 3.8, 7}; printf("saving article to binary file: %g %g %d\n", a1.price, a1.weight, a1.shipping); if(!Article_saveBin(&a1, "output_data")) { fprintf(stderr, "cannot save article to binary file\n"); } else { Article a2; if(!Article_loadBin(&a2, "output_data")) { fprintf(stderr, "cannot load article from binary file\n"); } printf("loaded article from binary file: %g %g %d\n", a2.price, a2.weight, a2.shipping); } }
void test_Solution(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Solution s1={12.25, 2, -2.0, -0.25}; // a=2 b=4.5 c=1 printf("saving solution to binary file: %g %d %g %g\n", s1.delta, s1.count, s1.x1, s1.x2); if(!Solution_saveBin(&s1, "output_data")) { fprintf(stderr, "cannot save solution to binary file\n"); } else { Solution s2; if(!Solution_loadBin(&s2, "output_data")) { fprintf(stderr, "cannot load solution from binary file\n"); } printf("loaded solution from binary file: %g %d %g %g\n", s2.delta, s2.count, s2.x1, s2.x2); } }
int main(void) { test_Duration(); test_Article(); test_Solution(); return 0; }
//----------------------------------------------------------------------------