我只是想收集一个HTML格式文件中的标题。
如果我这样编码,
...
int rd;
char buffer[BUFF_SIZE];
do
{
rd = read(0, buffer, BUFF_SIZE);
...
} while (rd == BUFF_SIZE);
...
那么无论如何我都可以读取文件直到 EOF,对吗?
我想在一个文件中找到“<title>”,但是,假设“
还是我的buffer读取逻辑有问题?请帮助我。
抱歉我的英语不好。我希望你们明白。
最佳答案
正如@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);
希望对你有帮助
关于c - 如何在 C 中没有库函数(使用 read() 系统调用)的文件中找到单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26544964/