开发者

Cycling through Text File and capture a string value [duplicate]

开发者 https://www.devze.com 2023-03-12 11:36 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Getting the Specified开发者_JAVA百科 content into the buffer in c
This question already has answers here: Closed 11 years ago.

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消