Thread: question about getenv...etc???

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    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]

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    Post

    Sorry about that, I'll make sure to remember it next time. I have gone ahead instead and attached my code.

    THanks for any help anyone can provide.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <iostream>
    #include <string.h>
    
    int 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
    getenv()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM