-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.memory_stream_write_operation.c
52 lines (43 loc) · 1.26 KB
/
06.memory_stream_write_operation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/***************************************************
*
* 描述:内存流的写入操作
*
* **************************************************
*/
#include <stdio.h>
#include <string.h>
#define BSZ 48
int main()
{
FILE *fp;
char buf[BSZ];
memset(buf, 'a', BSZ - 2);
buf[BSZ - 2] = '\0';
buf[BSZ - 1] = 'X';
if ((fp = fmemopen(buf, BSZ, "w+")) == NULL) {
printf("fmemopen error");
}
printf("initial buffer contents: %s\n", buf);
fprintf(fp, "hello, world");
printf("before flush : %s\n", buf);
fflush(fp);
printf("after flush : %s\n",buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
memset(buf, 'b', BSZ - 2);
buf[BSZ - 2] = '\0';
buf[BSZ - 1] = 'X';
fprintf(fp, "hello, world");
printf("before fseek : %s\n", buf);
fseek(fp, 0, SEEK_SET);
printf("after fseek : %s\n",buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
memset(buf, 'c', BSZ - 2);
buf[BSZ - 2] = '\0';
buf[BSZ - 1] = 'X';
fprintf(fp, "hello, world");
printf("before fclose : %s\n", buf);
fclose(fp);
printf("after fclose : %s\n",buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
return 0;
}