Advertisement
andruhovski

file_demo_2016_apr

Apr 7th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. // prog_file_demo.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. void demo01();
  7. void demo02();
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.     //demo01();
  11.     demo02();
  12.     return 0;
  13. }
  14.  
  15. void demo01()
  16. {
  17.     FILE *my_file;
  18.     errno_t err;
  19.     char symbol=0;
  20.     err = fopen_s(&my_file, "file_demo.txt", "rt");
  21.     if (err != 0)
  22.     {
  23.         printf("File not open, code %d", err);
  24.         return;
  25.     }
  26.     while ((symbol = fgetc(my_file)) != EOF)
  27.     {
  28.         putchar(symbol);
  29.     }
  30.     fclose(my_file);
  31. }
  32.  
  33. void demo02()
  34. {
  35.     FILE *my_file;
  36.     errno_t err;
  37.     //char str[90];
  38.     char *str = (char *)malloc(90);
  39.     if (str == NULL)
  40.     {
  41.         printf("No memory in demo02 %d", __LINE__);
  42.         return;
  43.     }
  44.     err = fopen_s(&my_file, "file_demo.txt", "rt");
  45.     if (err != 0)
  46.     {
  47.         printf("File not open, code %d", err);
  48.         return;
  49.     }
  50.     while (str = fgets(str, 89, my_file))
  51.     {
  52.         int idx = strlen(str) - 1;
  53.         if (str[idx] == '\n')
  54.             str[idx] = '\0';
  55.         puts(str);
  56.     }
  57.     fclose(my_file);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement