gpt4 book ai didi

c - 如何在C中逐行读取HTTP文件

转载 作者:行者123 更新时间:2023-11-30 20:35:58 24 4
gpt4 key购买 nike

我想从互联网上的纯文本文件中提取数据,并让它逐行读取。类似于使用 fgets() 逐行读取文件的方式。我不想下载该文件。我知道如果使用read()函数,可以指定接收多少字节并手动逐行读取文件。我只是想知道是否有任何方法可以自动执行此操作。感谢您的帮助!

最佳答案

我必须经常这样做,并编写了以下小函数来提供帮助。只需调用 FILE *wwwpopen(), *fp=wwwpopen("http://wherever.com/whatever.html ","r"); ,然后像往常一样使用 fp 来读取whatever.com 任何内容你喜欢的方式。完成后,您只需 pclose(fp) 即可。这是这个小函数(注意:如果 wget 不在您的默认路径上,则将下面的 wget[256] 初始化替换为系统上 wget 的完整路径),

/* ==========================================================================
* Function: wwwpopen ( char *url, char *mode )
* Purpose: popen("wget -O - url",mode)
* --------------------------------------------------------------------------
* Arguments: url (I) char * containing null-terminated string
* with url to be opened
* mode (I) char * containing null-terminated string
* that should always be "r"
* (this arg ignored, always "r"; just there for
* consistency with fopen/popen calling sequence)
* Returns: ( FILE * ) file pointer to popen()'ed url file
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o pclose(fileptr) yourself when finished reading file
* ======================================================================= */
/* --- entry point --- */
FILE *wwwpopen ( char *url, char *mode ) {
/* --- Allocations and Declarations --- */
FILE *fileptr = NULL; /* file ptr returned to caller */
char defaultmode[16] = "r", /* default mode, always used */
wgetargs[16] = "-O -", /* command-line args for wget */
wget[256] = "wget", /* replace by path to wget, if any */
command[512]; /* constructed wget command */
/* --- Check input --- */
mode = defaultmode; /* force popen() mode, must be "r" */
if ( url != NULL ) { /* url supplied */
/* --- popen() file --- */
sprintf(command,"%s %s %s", /* construct wget args url */
wget, /* path to wget program */
wgetargs, /* command-line args for wget */
url); /* and url to be wgotten */
fileptr = popen(command,mode); } /* popen() url (mode better be "r")*/
return ( fileptr ); /* back to caller with file ptr */
} /* --- end-of-function wwwpopen() --- */

关于c - 如何在C中逐行读取HTTP文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com