1 /*
2
3 * =====================================================================================
4
5 * Filename: fileutil.c
6
7 * Description:
8
9 * Version: 1.0
10
11 * Created: 2017年04月13日 09时38分23秒
12
13 * Revision: none
14
15 * Compiler: gcc
16
17 * Author: YOUR NAME (),
18
19 * Organization:
20
21 * =====================================================================================
22
23 */
24
25 #include
26
27 #include
28
29 #include <string.h>
30
31 #include "fileutil.h"
32
33
34
35 FILE *
36
37 open_file(const char *file,const char *mode)
38
39 {
40
41 FILE *fp;
42
43 if(!(fp = fopen(file,mode))){
44
45 perror("open file error");
46
47 exit(-1);
48
49 }
50
51 return fp;
52
53 }
54
55
56
57 void read0(const char *file)
58
59 {
60
61 FILE *fp = open_file(file,"r");
62
63 char buf[BUFSIZ] = {0};
64
65 unsigned long t = 0;
66
67 //int tmp = fread(buf,1,20,fp);
68
69 //printf("read %d bytes\n",tmp);
70
71 //printf("read buf from %s is %s\n",file,buf);
72
73 while((t = fread(buf,1,192,fp)) != 0){
74
75 printf("%s\n",buf);
76
77 bzero(&buf,sizeof(buf));
78
79 }
80
81 if(fclose(fp) != 0) perror("close file error");
82
83 }
84
85
86
87 void read1(const char *file)
88
89 {
90
91 FILE *fp = open_file(file,"r");
92
93 char *buf;
94
95 size_t n = 0;
96
97 while((n = getline(&buf,&n,fp)) != (size_t)-1){
98
99 printf("%s",buf);
100
101 bzero(buf,sizeof(buf));
102
103 }
104
105 //if(buf) free(buf);
106
107 if(fclose(fp) != 0) perror("close file error");
108
109 }
110
111
112
113 void read2(const char *file)
114
115 {
116
117 FILE *fp = open_file(file,"r");
118
119 char buf[BUFSIZ] = "";
120
121 while(fgets(buf,BUFSIZ,fp)){
122
123 printf("%s",buf);
124
125 bzero(buf,BUFSIZ);
126
127 }
128
129 if(fclose(fp) != 0) perror("close file error");
130
131 }
132
133
134
135 /* 尚未实现 */
136
137 void write1(const char *file)
138
139 {
140
141 FILE *fp = open_file(file,"a+t");
142
143 if(fclose(fp) != 0) perror("close file error");
144
145 }