我的结构如下
typedef struct
{
char *ptr;
size_t used;
size_t size;
} buffer;
typedef struct
{
buffer *request;
buffer *uri;
buffer *orig_uri;
http_method_t http_method;
http_version_t http_version;
buffer *request_line;
/* strings to the header */
buffer *http_host; /* not alloced */
const char *http_range;
const char *http_content_type;
const char *http_if_modified_since;
const char *http_if_none_match;
array *headers;
/* CONTENT */
size_t content_length; /* returned by strtoul() */
/* internal representation */
int accept_encoding;
/* internal */
buffer *pathinfo;
} request;
现在,如果我想(在文本文件中)写入属于结构“request”的成员“http_host”的值。成员“http_host”实际上是一个“缓冲”类型,我该怎么写呢?请用语法解释。
答案 0 :(得分:2)
假设你已经分配并初始化了所有相关的结构,你可以这样做:
request * req = malloc(sizeof(request));
buffer * buf = malloc(sizeof(buffer));
/* initialize buffer */
.......................
req->http_host = buf;
FILE * fp = fopen("file");
fprintf(fp,"ptr %s\n", req->http_host->ptr);
fprintf (fp,"size %d\n", req->http_host->size);
fprintf (fp,"used %d\n", req->http_host->used);
如果您想了解其他内容(即如果我误解了您的问题,请详细说明您的问题)
答案 1 :(得分:0)
你尝试过这样的事吗?
// open the file
FILE *fp = fopen("myfile.txt");
// print
fprintf(fp, "%s\n", http_host->ptr);
// close the file
fclose(fp);