From dbf45fa592b3e3cb09b8888a161712b4b9247742 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 8 Dec 2020 15:06:32 +0800 Subject: [PATCH] Finsh 5 --- 5/io/fmemopen.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 5/io/tmpfile.c | 2 -- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/5/io/fmemopen.c b/5/io/fmemopen.c index 33221da..8bb6e37 100644 --- a/5/io/fmemopen.c +++ b/5/io/fmemopen.c @@ -1,6 +1,52 @@ #include int main(void) { - // TODO + 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); + return 0; } diff --git a/5/io/tmpfile.c b/5/io/tmpfile.c index 370e851..0c68796 100644 --- a/5/io/tmpfile.c +++ b/5/io/tmpfile.c @@ -28,7 +28,5 @@ int main(void) { fclose(fp); - - return 0; }