Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // prog_file_demo.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- void demo01();
- void demo02();
- int _tmain(int argc, _TCHAR* argv[])
- {
- //demo01();
- demo02();
- return 0;
- }
- void demo01()
- {
- FILE *my_file;
- errno_t err;
- char symbol=0;
- err = fopen_s(&my_file, "file_demo.txt", "rt");
- if (err != 0)
- {
- printf("File not open, code %d", err);
- return;
- }
- while ((symbol = fgetc(my_file)) != EOF)
- {
- putchar(symbol);
- }
- fclose(my_file);
- }
- void demo02()
- {
- FILE *my_file;
- errno_t err;
- //char str[90];
- char *str = (char *)malloc(90);
- if (str == NULL)
- {
- printf("No memory in demo02 %d", __LINE__);
- return;
- }
- err = fopen_s(&my_file, "file_demo.txt", "rt");
- if (err != 0)
- {
- printf("File not open, code %d", err);
- return;
- }
- while (str = fgets(str, 89, my_file))
- {
- int idx = strlen(str) - 1;
- if (str[idx] == '\n')
- str[idx] = '\0';
- puts(str);
- }
- fclose(my_file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement