骑自行车通过文本文件,并获取一个字符串值
问题描述:
可能重复:
Getting the Specified content into the buffer in c骑自行车通过文本文件,并获取一个字符串值
我有一个文本文件,如下图所示。
<HTML>
<BODY>
Hello, User
<BODY>
<HTML>
我需要一个C程序捕获你好,用户到缓冲区变量.. 谁能请帮助我..谢谢
答
这是你的功课?如果是这样的话,你应该真的自己做这个......你将如何学习?您还应该提供您在发布问题时已完成的工作。
这是一个非常简单的解决方案。
#include <stdio.h>
#include <string.h>
#define SIZE_OF_BUFFER 255
#define FILENAME "yourpage.html"
#define TRUE 1
#define FALSE 0
/* Read the contents of the (first) BODY tag into buffer */
int main(void)
{
int next_read = FALSE;
char buffer[SIZE_OF_BUFFER] = {'\0'};
FILE *htmlpage;
htmlpage = fopen(FILENAME, "r");
if(htmlpage == NULL)
{
printf("Couldn't locate file - exiting...");
return -1;
}
while(fscanf(htmlpage, " %[^\n]s", buffer) != EOF)
{
if(strncmp(buffer, "<BODY>", 6) == 0)
{
next_read = TRUE;
}
else if(next_read == TRUE)
{
break;
}
}
fclose(htmlpage);
if(next_read == TRUE)
{
/* print the contents of buffer */
printf("%s\n", buffer);
}
else
{
printf("No <BODY> tag found in file!\n");
}
return 0;
}