Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

get Config file and language C

Hi

I have a config file (config.cfg) with something like:

Server_ID=3
DeleteDir=/home/delete/


in my application I have
char DeleteDir[500];
char Server_ID[100];

I'm trying to get the data from the config file and assign them to the var inside my application, someone have a function for that?
0
Treal514
Asked:
Treal514
  • 6
  • 2
1 Solution
 
akshayxxCommented:
here is one specifically for ur need

#include <stdio.h>
#include <string.h>
#define SOMEBUFSIZE 256

char DeleteDir[500];
char Server_ID[100];

main ()
{
  FILE *fp;
  char buf[SOMEBUFSIZE];
  char *idx, *end;
  fp = fopen ("config.cfg", "r");
  if (fp == NULL)
    {
      printf ("cfg file not found\n");
      exit (0);
    }
  while (fgets (buf, SOMEBUFSIZE, fp))
    {
      if (strstr (buf, "DeleteDir"))
     {
       idx = strchr (buf, '=');
       idx++;
// take care of spaces between '=' and value
       while (*idx == ' ')
         idx++;

       strcpy (DeleteDir, idx);
     }
      else if (strstr (buf, "Server_ID"))
     {

       idx = strchr (buf, '=');
       idx++;
// take care of spaces between '=' and value
       while (*idx == ' ')
         idx++;
       strcpy (Server_ID, idx);

     }

    }

       Server_ID[strlen(Server_ID)-1]='\0';
       DeleteDir[strlen(DeleteDir)-1]='\0';

printf("DeleteDir = %s \nServer_ID = %s\n",DeleteDir,Server_ID);
}
0
 
akshayxxCommented:
>>  Server_ID[strlen(Server_ID)-1]='\0';
>>  DeleteDir[strlen(DeleteDir)-1]='\0';

this i have done because ... fgets reads in the newline also .. so last character is a newline .. so replacing it with NULL character

one more thing in my program i dont take care of trailing spaces
if ur config file is like this


DeleteDir=  /home/delete/        <-- some spaces after the value

but in my program i am taking care of the potential spaces between '=' and actual value

let me know if u need to remove trailing spaces also

0
 
Treal514Author Commented:
thank you very much :) if you can add the "tralling spaces" for code it's can be great too :) I will wait for "Accept" your new code for other people, if you can't is not a problem, I will accept your first one :) thank you very much again :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
plusheyCommented:
While the solution given by akshayxx will work given the right circumstances, I must point out a few areas which could cause problems or where the code can be simplified.

Most importantly, the very last few lines of code where one attempts to remove the trailing '\n' from the strings will only work if indeed those strings have been found in the config.cfg file. If the config.cfg file did not contain the expected contents then the expressions will do something unexpected :-(. It is advisable to remove any trailing whitespace characters immediately after the string is copied or indeed to not copy them at all.

It is also noticeable that there is some repeated code in both parts of the IF within the WHILE. I find repeated code should be addressed at the earliest moment this will help if you ever add more variables to your file.

HTH
0
 
akshayxxCommented:
yes i accept what plushey said , i wrote the code in real hurry, all my intention was to make it work for 'YOUR CASE'
i forgot to mention that some error checking and fine tuning is left intentionally,
 So that in the process the asker may get the feel of the code, though its pretty straight forward and simple, better understanding comes when u get bugs and u solve them ... then only you get to know what works how , and in what scenarios..

wait a moment , i'll add the trailing spaces-removal and bit more of error checking ..
and after that i'll leave it to Treal514 to play with..
anyways thanks for the review--plushey
0
 
akshayxxCommented:
here is improved version..
also one thing remaining .. it still exepcts that any line of your will not be longer than 256 bytes..
if u expect line to be longer than 256 .. then change SOMEBUFSIZE..
and if the length can be indefinitely long .. ( say can be few MB long) then special routine will be require to readline,..
but in ur case of name value pairs .. i dont think u have indefinite limits..

good luck with following code..
now this also tells u if u come acroos unexpected lines ( without '=' )


#include <stdio.h>
#include <string.h>
#define SOMEBUFSIZE 256

char DeleteDir[500];
char Server_ID[100];

main ()
{
  FILE *fp;
  char buf[SOMEBUFSIZE];
  char *idx, *end;
  fp = fopen ("config.cfg", "r");
  if (fp == NULL)
    {
      printf ("cfg file not found\n");
      exit (0);
    }
  while (fgets (buf, SOMEBUFSIZE, fp))
    {
      idx = strchr (buf, '=');
      if (idx != NULL)
     {
       idx++;
       while (*idx == ' ')idx++;          //skipping spaces between '=' and value
       idx[strlen (idx) - 1] = '\0';     // removing last newline character

       end = idx + strlen (idx) - 1;
       while (*end == ' ') end--; // skipping trailing spaces
       *(++end) = '\0';  // first trailing space is made NULL

       if (strstr (buf, "DeleteDir"))
         {
           strcpy (DeleteDir, idx);
           //  DeleteDir[strlen(DeleteDir)-1]='\0';
         }
       else if (strstr (buf, "Server_ID"))
         {

           strcpy (Server_ID, idx);
           //  Server_ID[strlen(Server_ID)-1]='\0';

         }

     }
      else
     {
       printf ("invalid format of the name value pair\n");
       printf ("'=' expected in this line ==> \"%s\"\n", buf);
     }
    }

// before printing out u can check here whether u found required values OR not.
// by checking length of DeleteDir and Server_ID
// since DeleteDir and Server_ID are global variables they, if not filled, will have null
// if u intend to make them local variable .. then initialse to 0 length string.. before start parsing the config file

  printf ("DeleteDir = <%s> \nServer_ID = <%s>\n", DeleteDir, Server_ID);
// i added <> around %s for u to check whether trailing/leading spaces have been removed

}
0
 
akshayxxCommented:
Treal514:
let me know if my other version suits ur needs, that includes the trailing space-removal also..
if u have any problems let us know
0
 
Treal514Author Commented:
not any prob :) all work fine :) thank you very much again :)
0
 
akshayxxCommented:
u r welcome and good to know it works for u
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now