C语言-按字符读写文件demo程序
标签:define file dem secure clu crt 判断 char err
采用的是vs2019IDE来处理
1 //按字符读写文件
2
3 #define _CRT_SECURE_NO_WARNINGS
4 #include 5 #include 6 #includestring.h>
7 //按单个字符写文件
8 int Write_Char_File(FILE* fp)
9 {
10 fp = fopen("test.txt", "w");
11 if (fp == NULL)
12 {
13 perror("OPEN FILE TEST ERROR:");
14 printf("\n");
15 return -1;
16 }
17 //写26个英语字符放到文件当中
18 for (int i = 97; i 123; i++)
19 {
20 char a = i;
21 fputc(a, fp);
22 }
23 fclose(fp);
24 return 0;
25 }
26
27 //按单个字符读文件
28 int Get_Char_File(FILE* fp)
29 {
30 fp = fopen("D:/C_sum/test.txt", "r+");
31 if (fp == NULL)
32 {
33 perror("FUCK:");
34 printf("\n");
35 return -1;
36 }
37 char a;
38 while (1)
39 {
40 a = fgetc(fp);
41 if (a == EOF)
42 {
43 break;
44 }
45 else
46 {
47 putchar(a);
48 }
49 }
50 return 0;
51 fclose(fp);
52 }
53
54 //利用函数feof来判断是否将文件内容读取完
55 void Feof_Get_Char_File(FILE* fp)
56 {
57 fp = fopen("D:/C_sum/test.txt", "r");
58 if (fp == NULL)
59 {
60 perror("open file error:");
61 printf("\n");
62 return;
63 }
64 while (1)
65 {
66 char ch;
67 ch = fgetc(fp);
68 if (feof(fp))
69 {
70 printf("读取完毕\n");
71 break;
72 }
73 else
74 {
75 printf("%c",ch);
76 }
77 }
78 fclose(fp);
79 }
80
81 int main()
82 {
83 FILE* fp = NULL;
84 Feof_Get_Char_File(fp);
85 return 0;
86 }
C语言-按字符读写文件demo程序
标签:define file dem secure clu crt 判断 char err
原文地址:https://www.cnblogs.com/beautiful7/p/13915853.html
评论