question about getenv...etc???
Hi,
I have been struggling to figure this out and would really appreciate any help anyone can give:
I have a config file:
setenv test_host somehost
setenv test_port 12345
setenv test_sub RRR:MMM
before I compile and run my configreader.c file, I type "source config" to set the environments.
Once that is done I compile and run my configreader.c file.
The configreader is supposed to cout the "somehost" value. the "12345" value, and "RRR:MMM" value. I use the getenv() command.
THe configreader is also supposed to parse out the RRR:MMM and put RRR and MMM into a char array or maybe a string so that I can refer to it somewhere else.
I have tried to use sprintf and strtok, but I very confused as to what to do to be able to parse "RRR:MMM" into 2 char arrrays or string.
Here is the code for configreader.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
main()
{
char *env;
int port = 0;
char *env2;
char buffer[50];
char *p;
char one[10];
char two[10];
/*this works*/
env = getenv("test_host");
cout<<"this is the host "<<env<<endl;
port = atoi(env);
cout<<"this is the port number "<< port<<endl;
/* QUESTION HERE???? */
env2 = getenv("test_sub");
cout<<"this is the test_sub"<<env2<<endl;
int n = sprintf(buffer, "%s");
env2 = strtok (buffer, ":");
do
{
p = strtok(NULL, ":");
cout<<p<<endl;
one = p;
}while(p);
/* ?? want to be able to retrieve one and two which should contain RRR and MMM */
}//end main
THank you for any help anyone can provide to help me make this program work![CODE]