fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char rbuf[128];
  6. char* rstring = "Test1\n"
  7. "Test2\n"
  8. "Test30\n";
  9. char wbuf[128];
  10.  
  11. void create_file(void)
  12. {
  13. FILE* fp = fmemopen(rbuf, sizeof(rbuf), "w");
  14. fwrite(rstring, strlen(rstring) + 1, 1, fp);
  15. fclose(fp);
  16. }
  17.  
  18. void swap(char* a, char* b)
  19. {
  20. char c = *a;
  21. *a = *b;
  22. *b = c;
  23. }
  24.  
  25. char* strrev(char* str)
  26. {
  27. int i;
  28. for (i=0; i<strlen(str)/2; i++)
  29. swap(&str[i], &str[strlen(str)-1-i]);
  30. return str;
  31. }
  32.  
  33.  
  34. int main(void)
  35. {
  36. FILE *fp_r, *fp_w, *fp2;
  37. char* buf = malloc(sizeof(rbuf));
  38. char* buf2;
  39.  
  40. /* Create Dummy File */
  41. create_file();
  42.  
  43. /* Open Read/Write File */
  44. fp_r = fmemopen(rbuf, sizeof(rbuf), "r");
  45. fp_w = fmemopen(wbuf, sizeof(wbuf), "w");
  46.  
  47. while (!feof(fp_r)) {
  48. /* Read from Dummy Read File */
  49. fgets(buf, sizeof(rbuf), fp_r);
  50. /* Reverse string */
  51. buf[strlen(buf)-1] = NULL;
  52. strrev(buf);
  53. buf[strlen(buf)] = '\n';
  54. /* Write to Write File */
  55. fputs(buf, fp_w);
  56. }
  57.  
  58. free(buf);
  59. fclose(fp_r);
  60. fclose(fp_w);
  61.  
  62. /* Check */
  63. fp2 = fmemopen(wbuf, sizeof(wbuf), "r");
  64. buf2 = malloc(sizeof(wbuf));
  65. fread(buf2, sizeof(wbuf), 1, fp2);
  66. fprintf(stdout, "%s", buf2);
  67. free(buf2);
  68. fclose(fp2);
  69.  
  70. return EXIT_SUCCESS;
  71. }
  72.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
1tseT
2tseT
03tseT