繁体   English   中英

与具有成员 char *ptr 的类型混淆; 使用了 size_t; size_t 大小?

[英]Confusion with a type that has members char *ptr; size_t used; size_t size?

我的结构如下

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”其实是一个“buffer”类型,应该怎么写呢? 请用语法解释。

假设您已经分配并初始化了所有相关结构,您可以通过以下方式 go:

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

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

你试过这样的事情吗?

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

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

// close the file
fclose(fp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM