Advertisement
pexea12

FileIO

Feb 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. #include <string.h>
  18. //#define TRUE 1;
  19. //#define FALSE 0;
  20. /*
  21.  *
  22.  */
  23. void WriteFile(char* pFileName, char* pContent) {
  24.     int length = 0;
  25.  
  26.     FILE* f = fopen(pFileName, "a");
  27.     fflush(stdin);
  28.     do {
  29.         gets(pContent);
  30.         length = strlen(pContent);
  31.         if (length > 0) {
  32.             fputs(pContent, f);
  33.         }
  34.     } while (length > 0);
  35.     fclose(f);
  36. }
  37.  
  38. void ReadFile(char* pFileName) {
  39.     char c;
  40.     FILE* f = fopen(pFileName, "r");
  41.     while ((c = fgetc(f)) != EOF)
  42.         putchar(c);
  43.     fclose(f);
  44. }
  45.  
  46. int 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
Advertisement