StudyingUnixInterface/5/io/fmemopen.c

53 lines
728 B
C
Raw Normal View History

2020-12-08 04:24:52 +00:00
#include <stdio.h>
int main(void) {
2020-12-08 07:06:32 +00:00
char buf[1024];
FILE *fp = fmemopen(buf, sizeof(buf), "r+");
fprintf(fp, "Hello World!");
fseek(fp, 0, SEEK_SET);
char buf2[1024];
fscanf(fp, "%s", buf2);
printf("buf2: %s\n", buf2);
fclose(fp);
char *pbuf = NULL;
size_t buf_size = 1024u;
FILE *fp2 = open_memstream(&pbuf, &buf_size);
if(fp2 == NULL) {
printf("open_memstream error\n");
return -1;
}
for(int i = 0; i < 2048; i++){
fprintf(fp2, "TTTest ");
}
printf("pbuf: %p\n", pbuf);
fflush(fp2);
printf("pbuf: %p\n", pbuf);
for(int i = 0; i < 8192; i++){
fprintf(fp2, "TTTest ERRRRR");
}
fflush(fp2);
printf("pbuf: %p\n", pbuf);
fclose(fp2);
2020-12-08 04:24:52 +00:00
return 0;
}