簡體   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