Reading File Data after Keyword

Hi,

basically what I am trying to do is open a file in C and read all characters only after a sequence of characters that I specify. For example, the file would have a lot of characters of indeterminate size and after it would be the unique keyword "KEYWORD", then more characters. What would be the best way to program it to search for the unique "KEYWORD" and only store the characters after it into a buffer? I appreciate any help.
You understand that you have to read the file to find the KEYWORD, right?
Thanks for the reply. Could you please elaborate? I'm still new to programming with files. I think I know until doing a fread function on the entire file. What do you suggest I do from there?
Use an fstream.
Read through the file until you find your KEYWORD, going line by line I guess? Then from there, keep reading, but track the data and store it into something besides a temporary.
http://www.cplusplus.com/doc/tutorial/files/
Thanks. I have attempted to experiment with some of the code. I have a file named test.txt and it has only one character in it, which is a 6. As a test, I attempt to write a function to open the file and detect that the first character in it is 6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FILE* pFile;
char* cBuffer;
    long lSize;

pFile = fopen(szFileName, "rb");

    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    rewind(pFile);

    cBuffer = (char*)malloc(sizeof(char)*lSize);

    fread(cBuffer, 1, 1, pFile);

    if(cBuffer[0] == 6)
        return 1;

    free(cBuffer);

    fclose(pFile);


It doesn't appear to work as it does not return 1. If somebody could read over this short code segment and tell me of any obvious mistakes and/or what I'm doing wrong I'd greatly appreciate it.
Characters are numbers, but the number 6 is different than the character '6'.
15
16
    if(cBuffer[0] == '6')
        return 1;

In C, you can search for your keyword using the strstr() function in <string.h>
http://www.cplusplus.com/reference/clibrary/cstring/strstr/

1
2
3
4
5
6
    char* cKeyword;        /* This is where the keyword is */
    char* cResult = NULL;  /* This is the text following the keyword */
    ...
    cKeyword = strstr(cBuffer,"akeyword");
    if (cKeyword != NULL)
        cResult = cKeyword+strlen("akeyword");


One final note. On line 16 you return before you free() the buffer and fclose() the file. Don't do that. It is a memory leak and a resource leak.

Personally, I would rearrange things like this:
1
2
3
4
5
6
7
8
9
10
    ...
    cBuffer = (char*)malloc(...);
    fread(cBuffer, 1, 1, pFile);
    fclose(pFile);

    cKeyword = strstr(...);

    ... (do what you want with cResult here)

    free(cBuffer);

Hope this helps.
Thanks, strstr() appears to be just the function I was looking for. I'm having trouble understanding how to detect the characters after obtaining cResult. Here is a code segment of my attempt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    char* cBuffer, *cKeyword, *cResult;

...

    cKeyword = strstr(cBuffer, "KEYWORD"); //scan "KEYWORD" in the buffer
    if(cKeyword != NULL)
        cResult = cKeyword + strlen("KEYWORD");
    else
    {
        free(cBuffer);
        return 1;
    }

    if(cResult=="TEXT")
    {
        free(cBuffer);
        return 1;
    }

...


The file has only the characters "KEYWORDTEXT" without the quotes. I am trying to detect the characters after KEYWORD is TEXT. I tried testing if cResult equals "TEXT" but the function does not return 1. Any suggestions?
Topic archived. No new replies allowed.