Loading lang_c_ex_0801...
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 STRUCTFORMAT_H #define STRUCTFORMAT_H
#include <stdbool.h>
typedef struct { int hour; int minute; int second; double subsecond; } Duration;
void Duration_write(const Duration *duration, char *txt, int capacity);
bool // success Duration_read(Duration *out_duration, const char *txt);
typedef struct { double price; double weight; int shipping; } Article;
void Article_write(const Article *article, char *txt, int capacity);
bool // success Article_read(Article *out_article, const char *txt);
typedef struct { double delta; int count; double x1; double x2; } Solution;
void Solution_write(const Solution *solution, char *txt, int capacity);
bool // success Solution_read(Solution *out_solution, const char *txt);
#endif // STRUCTFORMAT_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "structFormat.h"
#include <stdio.h>
void Duration_write(const Duration *duration, char *txt, int capacity) { snprintf(txt, capacity, "%d %d %d %g", duration->hour, duration->minute, duration->second, duration->subsecond); }
bool // success Duration_read(Duration *out_duration, const char *txt) { return sscanf(txt, "%d %d %d %lg", &out_duration->hour, &out_duration->minute, &out_duration->second, &out_duration->subsecond)==4; }
void Article_write(const Article *article, char *txt, int capacity) { snprintf(txt, capacity, "%g %g %d", article->price, article->weight, article->shipping); }
bool // success Article_read(Article *out_article, const char *txt) { return sscanf(txt, "%lg %lg %d", &out_article->price, &out_article->weight, &out_article->shipping)==3; }
void Solution_write(const Solution *solution, char *txt, int capacity) { snprintf(txt, capacity, "%g %d %g %g", solution->delta, solution->count, solution->x1, solution->x2); }
bool // success Solution_read(Solution *out_solution, const char *txt) { return sscanf(txt, "%lg %d %lg %lg", &out_solution->delta, &out_solution->count, &out_solution->x1, &out_solution->x2)==4; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "structFormat.h"
#include <stdio.h>
void test_Duration(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char txt[100]; const Duration d1={1, 5, 17, 0.854}; printf("writing duration: %d %d %d %g\n", d1.hour, d1.minute, d1.second, d1.subsecond); Duration_write(&d1, txt, sizeof(txt)); printf("--> {%s}\n", txt); Duration d2; if(!Duration_read(&d2, txt)) { fprintf(stderr, "cannot read duration\n"); } else { printf("read duration: %d %d %d %g\n", d2.hour, d2.minute, d2.second, d2.subsecond); } }
void test_Article(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char txt[100]; const Article a1={49.99, 3.8, 7}; printf("writing article: %g %g %d\n", a1.price, a1.weight, a1.shipping); Article_write(&a1, txt, sizeof(txt)); printf("--> {%s}\n", txt); Article a2; if(!Article_read(&a2, txt)) { fprintf(stderr, "cannot read article\n"); } else { printf("read article: %g %g %d\n", a2.price, a2.weight, a2.shipping); } }
void test_Solution(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char txt[100]; const Solution s1={12.25, 2, -2.0, -0.25}; // a=2 b=4.5 c=1 printf("writing solution: %g %d %g %g\n", s1.delta, s1.count, s1.x1, s1.x2); Solution_write(&s1, txt, sizeof(txt)); printf("--> {%s}\n", txt); Solution s2; if(!Solution_read(&s2, txt)) { fprintf(stderr, "cannot read solution\n"); } else { printf("read solution: %g %d %g %g\n", s2.delta, s2.count, s2.x1, s2.x2); } }
int main(void) { test_Duration(); test_Article(); test_Solution(); return 0; }
//----------------------------------------------------------------------------