<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
一直希望有个可以像 FILE* 一样使用的 memory file,正好,今天,在linux的stdio.h中找到了这个东西。
#define _GNU_SOURCE
#include <stdio.h>
FILE *fmemopen(void *buf, size_t size, const char *mode);
FILE *open_memstream(char ** ptr, size_t *sizeloc) ;
详细说明:http://linux.die.net/man/3/open_memstream
fmemopen 有用之处主要在于从内存中读取,使用 fscanf。当然也可以写,如果是为了写,并且随后再读,可以将 buf 和 size指定为 NULL,0,这样写时会自动增加内存。
open_memstream 就主要用于写了,比如生成sql语句:
int i; char* sql = NULL; size_t len = 0; FILE* mf = open_memstream(&sql, &len); fprintf(mf, "insert into test(a,b,c) values"); for (i = 0; i < 100; ++i) fprintf(mf, "(%d,%d,%d),", i, i*i, i*i*i); fclose(mf); // write a \0 at the end of sql, now len==strlen(sql) sql[len-1] = 0; // trim last ',' execute(sql); free(sql); // sql should be freed by the caller