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 7712797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:43:10+00:00 2026-06-01T01:43:10+00:00

I have seen config files(yes, text files) for various console applications that look like

  • 0

I have seen “config” files(yes, text files) for various console applications that look like the following

<token> <value>

How would I go about parsing such a thing in C, where < value > could be a string, a letter or even an integer/float/double?

I read this question ” How do I parse a token from a string in C? ” where it is mostly recommended to use strtok, but also that it’s not thread-safe, and I am planning on spawning multiple threads(provided I am able to finish my application)

P.S
Here is an example of a token and a value

user username123
pass 123456

Ah, I forgot the tricky part. I must also be able to parse a token, which has multiple values either separated by a comma.

  • 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-01T01:43:11+00:00Added an answer on June 1, 2026 at 1:43 am

    I think that fgets() and sscanf() are your friend:

    int parseTokens(FILE *filePtr, char **tokens, char **values)
    {
        int i = 0;
    
        char line[128];
    
        while (fgets(line, 127, filePtr)) {
            tokens[i] = malloc(64);
            values[i] = malloc(64);
    
            sscanf(line, "%s %s", tokens[i], values[i]);
    
            i++;
        }
    
        return i;
    }
    
    int main(void)
    {
        char *tokens[20];
        char *values[20];
    
        FILE *filePtr = fopen("~/test.txt", "r");
    
        if (!filePtr)
        {
            fprintf(stderr, "Error opening file: %s", strerror(errno));
        }
    
        int count = parseTokens(filePtr, tokens, values);
    
        for (int i = 0; i < count; i++) {
            printf("%s %s\n", tokens[i], values[i]);
    
            free(tokens[i]);
            free(values[i]);
        }
    
        fclose(filePtr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen the references to VistaDB over the years and with tools like
I have seen this problem arise in many different circumstances and would like to
I have seen the official demos on lwjgl.org but I would like to see
I've written a simple config file parser in yacc, which processes files like this:
I have seen commands like this all over Makefiles, which I don't quite understand:
On my site I have a config file which has many settings that need
I have seen this, which suggests parsing the config information directly: passing **settings info
I have a command line application that reads some settings from the app.config file,
I want to match against key/value assignments in shell scripts, config files, etc., which
I have various values and names stored in a db table, that my code

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.