Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:59:59+00:00 2026-06-05T21:59:59+00:00

I have to develop a simple parser, to read block of text for example:

  • 0

I have to develop a simple parser, to read “block” of text
for example:

/TEST
 {. text .}
/TEST_DATA
 {. infs .}

and, I need to read informations of inside of label….
and… the file with have this informations… have a lot of labels, with the same perfil

for example:

/TEST
 {. text .}
/TEST_DATA
 {. infs .}

/LBL1
 {. text .}
/LBL1_DATA
 {. infs .}

/LBL2
 {. text .}
/LBL2_DATA
 {. infs .}

/LBL3
 {. text .}
/LBL3_DATA
 {. infs .}

i need to read the block of specif label, for example:

parseFile(“FileName.txt”, LBL1)

and the function, return for me, the text of inside the blocks: LBL1 and LBL1_DATA
or, return for me, the content of LBL1 and LBL1_DATA

I don’t know, how I can do this :xxx
I need help ;x

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:00:02+00:00Added an answer on June 5, 2026 at 10:00 pm

    Assuming data in one-line, here’s a very simple code example.

    You need, obviously test it, test it and test it. Looking to your behavior, fixing possible bugs and something that I forgot to do(it’s a work for you now) and so do your new implementations.

    int main(void)
    {
        const char *key = "TEST";
        const char *filename = "file";
    
        char *val = get(filename, key);
        if(val) {
            printf("%s\n", val); // {. text .}
            free(val); // don't to forget!
        } else {
            printf("'%s' was not found.\n", key);
        }
    
    
    }
    
    
    char*
    get(const char *filename, const char *key) {
    
        char *line = NULL, *pline = NULL, *buf = NULL, *pbuf, *tbuf;
        size_t size = -1, ssearch = strlen(key), i = 0, bufsize = 256;
        int open = 0;
        FILE *fp = fopen(filename, "r");
    
        if(fp == NULL) {
            fprintf(stderr, "Cannot read '%s' file.\n", filename);
            exit(EXIT_FAILURE);
        }
    
        while(getline(&line, &size, fp) != -1) {
    
            if(open == 0 && *line == '/' && 
               strncmp(line + 1, key, ssearch) == 0 && isspace(((unsigned char)*(line + ssearch + 1)))) {
                open = 1;
                continue;
            }
    
            if(open) {
                pline = line;
    
                while(isspace((unsigned char) *pline)) ++ pline; /* strip white-spaces [\r\n\t\v ] */
    
                if(*pline == '{') {
                    if((buf = malloc(bufsize)) == NULL) {
                        fprintf(stderr, "NO MEMORY!");
                        exit(EXIT_FAILURE);
                    }
    
                    ++pline; /* strip '{' */
    
                    pbuf = buf;
                    while(1) {
    
                        if(*pline == '\0') {
                            fprintf(stderr, "EOF but '{' was not closed.");
                            exit(EXIT_FAILURE);
                        }
    
                        /* etc.. */
    
                        if(*pline == '}') break; 
    
                        if((i + size + 1) >= bufsize) {
    
                            if((tbuf = realloc(buf, bufsize + size + 1)) == NULL) {
                                if(buf) free(buf);
                                fprintf(stderr, "No MEMORY!\n");
                                exit(EXIT_FAILURE);
                            }
    
                            buf = tbuf;
                        }   
    
                        *pbuf ++= *pline++, 
                        i ++;
                    }   
    
                    *pbuf ++= '\0';
    
                    if(pline != NULL) 
                        free(line);
    
                    fclose(fp);
    
                    return buf;
    
                } else {
                    fprintf(stderr, "expected '{' but '%c' was found.\n", *pline);
                    exit(EXIT_FAILURE);
                }
            }
    
            line = NULL;
            size = -1;
        }
    
        if(line != NULL)
            free(line);
    
        fclose(fp);
    
    
        return NULL;
    }
    

    UPDATE: a more simple code was written.

    #define EXPECTEDSYMBOL(w, f) \
            fprintf(stderr, "expected '%c' but '%c' was found.\n", w, f); \
            exit(EXIT_FAILURE)
    
    char* get2(const char *filename, const char *key) {
    
        char *line = NULL, *buf = NULL, *pline;
        size_t size = -1, ssearch = strlen(key);
        int open = 0;
        FILE *fp = fopen(filename, "r");
    
        if(fp == NULL) {
            fprintf(stderr, "Cannot read '%s' file.\n", filename);
            exit(EXIT_FAILURE);
        }
    
        while(getline(&line, &size, fp) != -1) {
    
            if(open == 0 && *line == '/' && 
               strncmp(line + 1, key, ssearch) == 0 && isspace(((unsigned char)*(line + ssearch + 1)))) {
                open = 1;
                continue;
            }
    
            if(open) {
    
                pline = line;
    
                while(isspace((unsigned char) *pline)) ++ pline;
    
                if(*pline != '{') {
                    EXPECTEDSYMBOL('{', *pline);
                } 
    
                if(strchr(pline, '}') == NULL) {
                    EXPECTEDSYMBOL('}', *(pline + strlen(pline) - 1));
                }
    
                buf = pline;
    
                break;
            }
    
            line = NULL;
        }
    
        fclose(fp);
    
    
        return buf;
    
    }
    

    I hope this help you.

    EDIT #2: I read again your question and seen that you need of below key as well.

    Try this:

    void
    get(const char *filename, const char *key, char buf[][512]) {
    
        char *line = NULL;
        size_t size = -1, ssearch = strlen(key);
        int open = 0;
    
        FILE *fp = fopen(filename, "r");
    
        if(fp == NULL) {
            fprintf(stderr, "Cannot read '%s' file.\n", filename);
            exit(EXIT_FAILURE);
        }
    
        while(getline(&line, &size, fp) != -1) {
    
            if(open == 0 && *line == '/' && 
               strncmp(line + 1, key, ssearch) == 0 && isspace(((unsigned char)*(line + ssearch + 1)))) {
                open = 1;
                continue;
            }
    
            if(open == 1) {
                strcpy(buf[0], line);
                ++ open;
                continue;
            }
    
            if((open + 1) == 3) {
                getline(&line, &size, fp);
                strcpy(buf[1], line);
                break;
            }
        }
    
        fclose(fp); 
    
    }
    

    And then:

    const char *key = "TEST"; // /TEST
    const char *filename = "config";
    char buf[2][512] = { { 0 } };
    
    get(filename, key, buf);
    printf("%s\n", buf[0]); //  {. text . }
    printf("%s\n", buf[1]); //  {. infs .}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to develop a pretty simple php website so I don't need framework.
My i have to develop simple window based iphone application. In my first screen
I am new to OpenGL. I have to develop a simple 3D application. I
i have some WinForms app (Framework to develop some simple apps), written in C#.
i have some WinForms app (Framework to develop some simple apps), written in C#.
My question is too simple but puzziling. i have a develop a mvc pages
I have simple MongoDB collection and I am using SLIM micro-framework to develop a
I'm trying to develop simple Python (3.2) code to read XML files, do some
I want to develop a simple iPhone application. However, I do not have the
I have to develop simple mail client in symfony2 using IMAP. Im wondering what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.