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

char rbuf[128];
char* rstring = "Test1\n"
                "Test2\n"
                "Test30\n";
char wbuf[128];

void create_file(void)
{
  FILE* fp = fmemopen(rbuf, sizeof(rbuf), "w");
  fwrite(rstring, strlen(rstring) + 1, 1, fp);
  fclose(fp);
}

void swap(char* a, char* b)
{
  char c = *a;
  *a = *b;
  *b = c;
}

char* strrev(char* str)
{
  int i;
  for (i=0; i<strlen(str)/2; i++)
    swap(&str[i], &str[strlen(str)-1-i]);
  return str;
}


int main(void)
{
  FILE *fp_r, *fp_w, *fp2;
  char* buf = malloc(sizeof(rbuf));
  char* buf2;

  /* Create Dummy File */
  create_file();

  /* Open Read/Write File */
  fp_r = fmemopen(rbuf, sizeof(rbuf), "r");
  fp_w = fmemopen(wbuf, sizeof(wbuf), "w");

  while (!feof(fp_r)) {
    /* Read from Dummy Read File */
    fgets(buf, sizeof(rbuf), fp_r);
    /* Reverse string */
    buf[strlen(buf)-1] = NULL;
    strrev(buf);
    buf[strlen(buf)] = '\n';
    /* Write to Write File */
    fputs(buf, fp_w);
  }

  free(buf);
  fclose(fp_r);
  fclose(fp_w);

  /* Check */
  fp2 = fmemopen(wbuf, sizeof(wbuf), "r");
  buf2 = malloc(sizeof(wbuf));
  fread(buf2, sizeof(wbuf), 1, fp2);
  fprintf(stdout, "%s", buf2);
  free(buf2);
  fclose(fp2);

  return EXIT_SUCCESS;
}
