如何在C中逐行读取HTTP文件
我想从互联网上的纯文本文件中提取,并让它逐行读取。类似于使用fgets()逐行读取文件的方式。我不想下载这个文件。我知道,如果使用read()函数,可以指定接收多少字节,并逐行手动读取文件。我只是想知道是否有什么方法可以自动做到这一点。谢谢你的帮助 我必须经常这样做,并编写了下面的小函数来提供帮助。只需调用文件*wwwpopen(),*fp=wwpopen(“,”r”)然后像往常一样使用fp以任何你喜欢的方式阅读任何网站。完成后,只需关闭(fp)您自己。这是一个小函数(注意:如果wget不在默认路径上,那么用系统上wget的完整路径替换下面的wget[256]初始化)如何在C中逐行读取HTTP文件,c,http,C,Http,我想从互联网上的纯文本文件中提取,并让它逐行读取。类似于使用fgets()逐行读取文件的方式。我不想下载这个文件。我知道,如果使用read()函数,可以指定接收多少字节,并逐行手动读取文件。我只是想知道是否有什么方法可以自动做到这一点。谢谢你的帮助 我必须经常这样做,并编写了下面的小函数来提供帮助。只需调用文件*wwwpopen(),*fp=wwpopen(“,”r”)然后像往常一样使用fp以任何你喜欢的方式阅读任何网站。完成后,只需关闭(fp)您自己。这是一个小函数(注意:如果wget不在默认
使用
http
您将下载该文件,而不管它是否在内存中。我想您的意思是说“您不想将文件保存到磁盘”?该过程分为两步,使用curl
读取网页,然后通过一个单独的htm2text
文件处理文本,您可以使用execlp
或类似程序,使用dup2
创建/关闭所需的多个文件描述符。显然,我的意思是我不想将文件保存到磁盘。没有理由做个聪明人
/* ==========================================================================
* 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() --- */