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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:24:08+00:00 2026-05-18T04:24:08+00:00

gcc 4.5.1 c89 I am using the following code to read in a line

  • 0

gcc 4.5.1 c89

I am using the following code to read in a line of text from a configuration file. The configuration file is small at the moment, will grow with new fields to add. And I can pretty much design what the configuration file will look like myself. So I have done it in the following way:

config.cfg

# Configuration file to be loaded 

# protocol to use either ISDN, SIP, 
protocol: ISDN

# run application in the following mode, makecall or waitcall
mode: makecall

I have used the colon as a way to search for the type of configuration that will be needed. I am just wondering if there a better way to do this?

My code I have used is as follows:

static int load_config(FILE *fp)
{
    char line_read[LINE_SIZE] = {0};
    char *type = NULL;
    char field[LINE_SIZE] = {0};
    char *carriage_return = NULL;

    /* Read each line */
    while(fgets(line_read, LINE_SIZE, fp) != NULL) {
        if(line_read != NULL) {
            /* Ignore any hashs and blank lines */
            if((line_read[0] != '#') && (strlen(line_read) > 1)) {
                /* I don't like the carriage return, so remove it. */
                carriage_return = strrchr(line_read, '\n');
                if(carriage_return != NULL) {
                    /* carriage return found so relace with a null */
                    *carriage_return = '\0';
                }

                /* Parse line_read to extract the field name i.e. protocol, mode, etc */
                parse_string(field, line_read);
                if(field != NULL) {
                    type = strchr(line_read, ':');
                    type+=2; /* Point to the first character after the space */

                    if(strcmp("protocol", field) == 0) {
                        /* Check the protocol type */
                        printf("protocol [ %s ]\n", type);
                    }
                    else if (strcmp("mode", field) == 0) {
                        /* Check the mode type */
                        printf("mode [ %s ]\n", type);
                    }
                }
            }
        }
    }

    return TRUE;
}

/* Extract the field name for the read in line from the configuration file. */
static void parse_string(char *dest, const char *string)
{
    /* Copy string up to the colon to determine configuration type */
    while(*string != ':') {
        *dest++ = *string++;
    }
    /* Insert nul terminator */
    *dest = '\0';
}
  • 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-05-18T04:24:09+00:00Added an answer on May 18, 2026 at 4:24 am

    easier should be:

    static int load_config(FILE *fp)
    {
      int r=0;
      char line[LINE_SIZE], field[LINE_SIZE], type[LINE_SIZE], dummy[LINE_SIZE];
    
      /* Read each line */
      while( fgets(line, LINE_SIZE, fp) )
      {
        if( strchr(line,'\n') ) *strchr(line,'\n')=0;
        if( 3==sscanf(line,"%[^: ]%[: ]%s,field,dummy,type) )
          ++r,printf("\n %s [ %s ]",field,type);
      }
      return r;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using gcc 4.4.3 c89: I am just working on the following code below: I
gcc 4.4.3 c89 I have the following source file. However, I get a stack
gcc 4.4.4 c89 I am using the code below. However, I keep getting the
gcc 4.4.4 c89 I have always done the following when using structs to hide
gcc 4.4.4 c89 I have the following code and 2 structures that have to
The gcc -S option will generate assembly code in AT&T syntax, is there a
I'm using GCC to generate a dependency file, but my build rules put the
I'm using GCC; __FILE__ returns the current source file's entire path and name: /path/to/file.cpp
gcc 4.4.2 c89 I have this code snippet that I have to repeat in
gcc 4.4.4 c89 I am reading in a file and in that file I

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.