Loading lang_c_ex_0802...
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 TEXTINOUT_H #define TEXTINOUT_H
#include <stdbool.h>
typedef struct { int hour; int minute; int second; double subsecond; } Duration;
bool // success Duration_saveTxt(const Duration *duration, const char *fileName);
bool // success Duration_loadTxt(Duration *out_duration, const char *fileName);
typedef struct { double price; double weight; int shipping; } Article;
bool // success Article_saveTxt(const Article *article, const char *fileName);
bool // success Article_loadTxt(Article *out_article, const char *fileName);
typedef struct { double delta; int count; double x1; double x2; } Solution;
bool // success Solution_saveTxt(const Solution *solution, const char *fileName);
bool // success Solution_loadTxt(Solution *out_solution, const char *fileName);
#endif // TEXTINOUT_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "textInOut.h"
#include <stdio.h>
bool // success Duration_saveTxt(const Duration *duration, const char *fileName) { FILE *output=fopen(fileName, "w"); // text mode if(output==NULL) { return false; } fprintf(output, "%d %d %d %g", duration->hour, duration->minute, duration->second, duration->subsecond); fclose(output); return true; }
bool // success Duration_loadTxt(Duration *out_duration, const char *fileName) { FILE *input=fopen(fileName, "r"); // text mode if(input==NULL) { return false; } const bool success=fscanf(input, "%d %d %d %lg", &out_duration->hour, &out_duration->minute, &out_duration->second, &out_duration->subsecond)==4; fclose(input); return success; }
bool // success Article_saveTxt(const Article *article, const char *fileName) { FILE *output=fopen(fileName, "w"); // text mode if(output==NULL) { return false; } fprintf(output, "%g %g %d", article->price, article->weight, article->shipping); fclose(output); return true; }
bool // success Article_loadTxt(Article *out_article, const char *fileName) { FILE *input=fopen(fileName, "r"); // text mode if(input==NULL) { return false; } const bool success=fscanf(input, "%lg %lg %d", &out_article->price, &out_article->weight, &out_article->shipping)==3; fclose(input); return success; }
bool // success Solution_saveTxt(const Solution *solution, const char *fileName) { FILE *output=fopen(fileName, "w"); // text mode if(output==NULL) { return false; } fprintf(output, "%g %d %g %g", solution->delta, solution->count, solution->x1, solution->x2); fclose(output); return true; }
bool // success Solution_loadTxt(Solution *out_solution, const char *fileName) { FILE *input=fopen(fileName, "r"); // text mode if(input==NULL) { return false; } const bool success=fscanf(input, "%lg %d %lg %lg", &out_solution->delta, &out_solution->count, &out_solution->x1, &out_solution->x2)==4; fclose(input); return success; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "textInOut.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 text file: %d %d %d %g\n", d1.hour, d1.minute, d1.second, d1.subsecond); if(!Duration_saveTxt(&d1, "output_text")) { fprintf(stderr, "cannot save duration to text file\n"); } else { Duration d2; if(!Duration_loadTxt(&d2, "output_text")) { fprintf(stderr, "cannot load duration from text file\n"); } printf("loaded duration from text 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 text file: %g %g %d\n", a1.price, a1.weight, a1.shipping); if(!Article_saveTxt(&a1, "output_text")) { fprintf(stderr, "cannot save article to text file\n"); } else { Article a2; if(!Article_loadTxt(&a2, "output_text")) { fprintf(stderr, "cannot load article from text file\n"); } printf("loaded article from text 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 text file: %g %d %g %g\n", s1.delta, s1.count, s1.x1, s1.x2); if(!Solution_saveTxt(&s1, "output_text")) { fprintf(stderr, "cannot save solution to text file\n"); } else { Solution s2; if(!Solution_loadTxt(&s2, "output_text")) { fprintf(stderr, "cannot load solution from text file\n"); } printf("loaded solution from text 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; }
//----------------------------------------------------------------------------