Nguythang

FileIO

Feb 22nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: NgT
  10.  *
  11.  * Created on February 22, 2016, 10:16 AM
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. //#define TRUE 1;
  18. //#define FALSE 0;
  19. /*
  20.  *
  21.  */
  22. void Writefile(char* pFileName, char* pContent) {
  23.     int length = 0;
  24.  
  25.     FILE* f = fopen(pFileName, "a");
  26.     fflush(stdin);
  27.     do {
  28.         gets(pContent);
  29.         length = strlen(pContent);
  30.         if (length > 0) {
  31.             fputs(pContent, f);
  32.         }
  33.     } while (length > 0);
  34.     fclose(f);
  35. }
  36.  
  37. char* ReadFile(char* pFileName) {
  38.     char c;    
  39.     FILE* f = fopen(pFileName, "r");
  40.     while ((c = fgetc(f)) != EOF)
  41.         putchar(c);
  42.     fclose(f);
  43.     return 1;
  44. }
  45.  
  46. main() {
  47.     char filename[10], content[100];
  48.     printf("Enter file to save content: ");
  49.     gets(filename);
  50.     printf("Enter content: ");
  51.     gets(content);
  52.  
  53.     Writefile(filename, content);
  54.     printf("File content : \n");
  55.     ReadFile(filename);            
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment