Advertisement
TRToC

DemoSave() / DemoLoad()

Oct 21st, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #define MAXPUNKTE   16
  3.  
  4. void DemoSave() {
  5.     double feldX[MAXPUNKTE] = { 10, 20, 30, 40 };
  6.     double feldY[MAXPUNKTE] = { 15, 25, 35, 45 };
  7.     int feldLen = 4;
  8.  
  9.     FILE *file = fopen("demo.txt", "w");
  10.     if (file != NULL) {
  11.         for (int i = 0; i < feldLen; i++) {
  12.             fprintf(file, "%.2lf %.2lf\n", feldX[i], feldY[i]);
  13.         }
  14.         fclose(file);
  15.     }
  16. }
  17.  
  18. void DemoLoad() {
  19.     double feldX[MAXPUNKTE];
  20.     double feldY[MAXPUNKTE];
  21.     int feldLen = 0;
  22.  
  23.     FILE *datei = fopen("demo.txt", "r");
  24.     if (datei != NULL) {
  25.         while (fscanf(datei, "%lf %lf", &feldX[feldLen], &feldY[feldLen]) == 2) {
  26.             feldLen++;
  27.             if (feldLen >= MAXPUNKTE) {
  28.                 break; // too much
  29.             }
  30.         }
  31.         fclose(datei);
  32.     }
  33.    
  34.     for (int i = 0; i < feldLen; i++)
  35.     {
  36.         printf("%3i. Punkt: x : %lf\n", i + 1, feldX[i]);
  37.         printf("            y : %lf\n", feldY[i]);
  38.     }
  39. }
  40.  
  41. int main(void)
  42. {
  43.     DemoSave();
  44.     DemoLoad();
  45.     getchar();
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement