Link to home
Start Free TrialLog in
Avatar of eugene007
eugene007

asked on

Linux Red hat 9 - C language

int main(int argc, char *argv[])
{
         FILE *fptr;
         char buffer[100];
                         
         if(strcmp(argv[1] ,"-f"))
         {
                 printf("The first argument should be -f\n");
                 exit(0);
         }                          

         fptr = fopen(argv[2],"r");

         if(fptr == NULL)
         {
                 printf("Error opening the file\n");
                 exit(0);
         }
         else
         {
                 while(fgets(buffer,100,fptr))
                 {
                        if(getenv(buffer) != NULL)
                        {
                               printf("%s = %s\n",buffer,getenv(buffer));
                        }
                        else
                        {
                               printf("Error: There is no %s\n", buffer);
                        }              
                  }
          }
}


This retrieves the values from the text file. The text file contains:

pwd
hostname
nutsvariable
shell

it will retrive the value and if the environment variable exist, it will display the contents of the environment variable, or else it would display an error msg.

Problem: My program retrieves the value from the text file, however
when evaluated, it display an error msg, even thou the environment
variable exist.

How do I solve this problem.

Your help is kindly appreciated.

Regards
Eugene
Avatar of eugene007
eugene007

ASKER

Ive tried this code, which accepts the environment variable name from the user and display the value of the environment variable, and it works fine.

int main(int argc, char *argv[])
{                    
       if(getenv(argv[1]) != NULL)
       {
                printf("%s = %s\n",argv[1],getenv(argv[1]));
       }
       else
       {
               printf("%s not found\n", argv[1]);
       }
}


However when dealing with accepting the environment name from the file and evaluating it, it does not work. Just as the code ive specified.
hi...

  Which of those error msg is printed ???

Dennis
The error msg on this part:

printf("Error: There is no %s\n", buffer);

Error: There is no pwd
Error: There is no hostname
Error: There is no nutsvariable
Error: There is no shell

Regards
Eugene
Cause the result of of the getenv(buffer) is always (null) even thou the
environment variable exist.
If you write some debugging code that shows you exactly what characters are in the buffer array after the call the fgets, I think you will find that each of those strings contains a newline character ('\n').  (Alternately, you could carefully study the documentation on fgets.)  The environment variables don't contain newline characters, so the buffer strings don't match them.

--efn
ASKER CERTIFIED SOLUTION
Avatar of Ajar
Ajar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial