/*
  loes7-7.c
  Loesung der Aufgaben 7-6 bis 7-8
  7-6) Ersatztext der vordefinierten Makros
  7-7) Alle Grossbuchstaben einer Datei in Kleinbuchstaben umwandeln.
  7-8) Makro Bildstop.
*/


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define ENDLINE 0xA

#define StopOutput \
  { printf("Type RETURN to continue! "); getchar(); }

int main(void)
{
  char szFileIn[25], szFileOut[25];
  FILE *fin, *fout;
  int  c;
  int  nLinesPrinted = 2;

  printf ("Input-File : "); scanf ("%s", szFileIn);
  if ( (fin = fopen (szFileIn, "r")) == NULL) {
    printf("Could not open file %s\n");
    exit(1);
  }

  printf ("Output-File : "); scanf ("%s%*c", szFileOut);
  fout = fopen(szFileOut, "w");

  while ( (c = fgetc (fin)) != EOF) {
    if (isupper(c))
       c = tolower(c);
    fputc(c, stdout); // screen
    fputc(c, fout);   // file
    if (c == ENDLINE) {
      nLinesPrinted++;
      if (nLinesPrinted % 20 == 0)
        StopOutput
    }
  }

  fclose (fin);
  fclose (fout);

  printf("The 5 predefined makros:\n");
  printf("__DATE__ = %s\n", __DATE__);
  printf("__FILE__ = %s\n", __FILE__);
  printf("__LINE__ = %i\n", __LINE__);
  printf("__TIME__ = %s\n", __TIME__);
  printf("__STDC__ = %i\n", __STDC__);

  // this result in a message during compilation
  #if __STDC__ == 1
    #error A standard C implementation!
  #else
    #error Not a standard C implementation!
  #endif

  return 0;
}