
How to read a config file in C
Quote:
> > Ok. My draft is here:
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <unistd.h>
> > #include <string.h>
> > int main ()
> > {
> > FILE *CONF;
> > int buff_size = 1024;
> > char *buff, *token;
> > char delimiters[] = "= \t";
> > char **lineptr;
> > int *n;
> > lineptr = &buff;
> > n = &buff_size;
> > buff = (char*)malloc(*n);
> cast not needed :)
:))
> > if(getcwd(buff, buff_size) == 0){
> > printf("Can't get work directory.\nExit.\n");
> > exit(1);
> > }
> > strcat(buff, "/saturn.conf");
> this seems like a pretty complicated way to write:
> buff = "saturn.conf";
> :P
That's it! Thanks.
Quote:
> > if((CONF = fopen(buff, "r")) == NULL){
> > printf("Can't open config file: %s\nExit.\n", buff);
> > exit(1);
> EXIT_FAILURE is preferred by top breeders (and, incidentally, non-breeders
> and bottom breeders as well).
OK
Quote:
> > }
> > printf("Parse config file: %s\n", buff);
> > // free(buff);
> > while(getline(lineptr, n, CONF) != -1 ){
> getline? i'm not familiar with getline. is that similar to readline()?
It is a GNU extension but it is the recommended way to read lines from a
stream instead of standatd functions which are unreliable.
I've got this sentence from the reference manual of the GNU C Library :)
Quote:
> > token = strtok(buff, delimiters);
> > if(token[0] == '#' || token[0] == '\n') //Ignore comments and
> > empty lines
> > continue;
> > printf("%s ", token);
> > token = strtok(NULL, delimiters);
> > token[strlen(token) - 1] = '\0'; // Remove newline
> > printf("%s\n", token);
> > // and so on...
> > }
> > fclose(CONF);
> > free(buff);
> > exit(0);
> > }
> btw i might rewrite this last part just using fgets and sscanf():
> while (fgets(buff, n, CONF)) {
> buff[strlen(buff) - 1] = '\0'; /* remove possible '\n' */
> if (buff[0] == '#' || buff[0] == '\n') continue;
> if (sscanf(buff, "%s = %s", &var_name, &var_value)) {
> /* error */
> }
> }
sscanf() doesnot work, sorry. onf file doesnot strong formatted.
It's possible no spaces around token '='.
Quote:
> and then just decide how you want to store all the things (e.g. what're
> var_name and var_value going to be?).
I going to use strcmp() for var_name and sscanf() for var_value.
[SKIP]
Quote:
> > The questions are:
> > 1. Why gcc complains:
> > parse.c:33: warning: implicit declaration of function `getline'
> > despite getline() declarated in stdio.h ?
> #ifdef __USE_GNU
I put the command:
$ gcc -D__USE_GNU -Wall parse.c -o parse
and again get:
parse.c:38: warning: implicit declaration of function `getline'
What's wrong??
When I insert explicit declaration straight into the text of my program
this warning disappear.
Quote:
May be I will.
Quote:
> > 2. If we uncomment the firs line "free(buff);"
> > we catch SIGSEGV at the end of program (at the second "free(buff)").
> > Why?
> > I gess getline() must automatically use realloc to make the buffer
> > bigger.
> you guess? i think your guess is wrong. why would it require a size
> argument then?
Why? Cut from reference is here:
"getline(char **lineptr, size_t *n, FILE *stream)
Before calling getline(), you should place in *lineptr the address
of a buffer *n bytes long. If this buffer is long enough to hold
the line, getline() stores the line in this buffer. Otherwise,
getline() makes the buffer bigger using realloc(), storing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the new buffer address back in *lineptr and the increased
size back in *n."
Quote:
> > 3. How to force OS (Linux in my case) to do core dump file on error?
> i can't remember. it would be non-ANSI anyway. if worse comes to worse,
> you could just do:
> raise(SIGSEGV);
Oh, no. It is a little bit different thing. I don't need
to send a signal to myself. I would like to get core-dump file
after my program gets SIGSEGV from OS and dies.
I don't know should I insert something commands into
the text of my program for that. Or, may be I should
tune my OS? Any ideas?
Quote:
> (after including signal.h)
> --
> /"\ m i k e b u r r e l l
> X AGAINST HTML MAIL
> / \
To mike: Thank you for the lesson. I'm novice in C.
With regards,
Eugene Bobkov