?
Solved

get Config file and language C

Posted on 2003-03-11
9
Medium Priority
?
238 Views
Last Modified: 2013-11-18
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
Comment
Question by:Treal514
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8110047
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8110063
>>  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
 

Author Comment

by:Treal514
ID: 8114338
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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 

Expert Comment

by:plushey
ID: 8115877
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8116100
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
 
LVL 8

Accepted Solution

by:
akshayxx earned 1000 total points
ID: 8116203
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8119141
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
 

Author Comment

by:Treal514
ID: 8119458
not any prob :) all work fine :) thank you very much again :)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8119844
u r welcome and good to know it works for u
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question