Cycling through Text File and capture a string value [duplicate]
Possible Duplicate:
Getting the Specified开发者_JAVA百科 content into the buffer in c
I have a text file as shown Below.
<HTML>
<BODY>
Hello, User
<BODY>
<HTML>
I need a C program to Capture Hello, User into a buffer variable.. Can anyone please help me.. Thanks
Is this your homework? You should really be doing this yourself if it is... how will you learn otherwise? You should also provide what you have already done when you post a question.
Here is a very bare-bones solution.
#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;
}
精彩评论