0

我的结构如下

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;

现在,如果我想写入(在文本文件中)属于结构“请求”的成员“http_host”的值。成员“http_host”其实是一个“buffer”类型,应该怎么写呢?请用语法解释。

4

2 回答 2

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);

如果您想知道其他事情(也就是说,如果我误解了您的问题,请详细说明您的问题)

于 2012-04-05T10:30:58.590 回答
0

你有没有尝试过这样的事情?

// open the file
FILE *fp = fopen("myfile.txt");

// print
fprintf(fp, "%s\n", http_host->ptr);

// close the file
fclose(fp);
于 2012-04-05T10:31:54.197 回答