我只想收集HTML格式文件中的标题。
如果我这样编码,
...
int rd;
char buffer[BUFF_SIZE];
do
{
rd = read(0, buffer, BUFF_SIZE);
...
} while (rd == BUFF_SIZE);
...
那么我可以一直读取文件直到EOF,对不对?
我想在文件中找到“ <title>”,但是,假定在缓冲区的末尾读取“ <ti”,在下一个缓冲区的起始处读取“ tle>”。在这种情况下,如何找到“ <标题>”?
还是我的缓冲区读取逻辑有问题?请帮我。
对不起,我的英语不好。我希望你们能理解。
正如@Adraino提到的,您可以逐字符读取文件。
您只需要识别'<'
读取临时数组中的连续字符,直到遇到'>'
这样的情况:
int ch;
int i = 0;
do
{
ch=fgetc(file );
/*file is the file pointer*/
if(ch=='<')
{
while(ch!='>'||ch!=EOF)
{
ch=fgetc();
temp[i]=ch;
i++;
}
temp[i]='\0';
i=0;
}
if(strcmp(temp,"title")==0)
// read the next characters and store them in a string til '<' is encountered.
.
.
} while(ch!=EOF);
希望对您有所帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句