K
kb
Hi all,
I'm having trouble with the following code, of which I would like to
be able to get working without the use of string functions such as
strtok() and strcpy(), etc.. Please disregard off-topic nature of the
code where not pure C, as i believe the problem lies therein.
-------8<------
#include <stdio.h>
#include <stdlib.h>
#define MAXBUF 256
int main(void)
{
FILE *f;
char *d="cfg";
char *p;
char *q;
int x;
if((p=malloc(MAXBUF)) == NULL)
perror("malloc");
if((q=malloc(MAXBUF)) == NULL)
perror("malloc");
if((f=fopen(d,"r+")) < 0)
{
perror("fopen");
exit(1);
};
while(fgets(p,MAXBUF,f) != NULL)
{
while(*p != '\0')
{
if((*q++ = *p++) == '=')
{
printf("%s",q);
}
}
}
return 0;
}
------->8--------
I am trying to parse a flat file of the following form:
#conf file
rootdir=/test
opt1=foo
opt2=bar
And while it's easy to tokenize from the '=' and print the values
after that token, I am having a hard time consuming the chars before
the '='. Ouput is trash mostly, and i'm almost certain it's my use of
the 'q' pointer... where i doubt it points to what it should point to.
I've tried all manner of combination's of post/prefix to gobble up the
first part before the '=', but to no avail.
Any help is appreciated -
cheers
kB.
I'm having trouble with the following code, of which I would like to
be able to get working without the use of string functions such as
strtok() and strcpy(), etc.. Please disregard off-topic nature of the
code where not pure C, as i believe the problem lies therein.
-------8<------
#include <stdio.h>
#include <stdlib.h>
#define MAXBUF 256
int main(void)
{
FILE *f;
char *d="cfg";
char *p;
char *q;
int x;
if((p=malloc(MAXBUF)) == NULL)
perror("malloc");
if((q=malloc(MAXBUF)) == NULL)
perror("malloc");
if((f=fopen(d,"r+")) < 0)
{
perror("fopen");
exit(1);
};
while(fgets(p,MAXBUF,f) != NULL)
{
while(*p != '\0')
{
if((*q++ = *p++) == '=')
{
printf("%s",q);
}
}
}
return 0;
}
------->8--------
I am trying to parse a flat file of the following form:
#conf file
rootdir=/test
opt1=foo
opt2=bar
And while it's easy to tokenize from the '=' and print the values
after that token, I am having a hard time consuming the chars before
the '='. Ouput is trash mostly, and i'm almost certain it's my use of
the 'q' pointer... where i doubt it points to what it should point to.
I've tried all manner of combination's of post/prefix to gobble up the
first part before the '=', but to no avail.
Any help is appreciated -
cheers
kB.