Solved

Linux Red hat 9 - C language

Posted on 2003-10-26
6
205 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:eugene007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:eugene007
ID: 9622035
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.
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9622012
hi...

  Which of those error msg is printed ???

Dennis
0
 

Author Comment

by:eugene007
ID: 9622029
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:eugene007
ID: 9622031
Cause the result of of the getenv(buffer) is always (null) even thou the
environment variable exist.
0
 
LVL 15

Expert Comment

by:efn
ID: 9622951
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
0
 
LVL 6

Accepted Solution

by:
Ajar earned 150 total points
ID: 9625811
The problem is that fgets() copies the new line character to the end of the string too...

so to solve the problem do like this
------
--------
while(fgets(buffer,100,fptr))
                 {

                   char *temp = buffer;
                   while(*temp !='\n' && *temp !='\0') temp++;
                   temp--; if(*temp=='\n') *temp  ='\0';

                        if(getenv(buffer) != NULL)
                        {
                               printf("%s = %s\n",buffer,getenv(buffer));
                        }
                        else
                        {
                               printf("Error: There is no %s\n", buffer);
                        }              
                  }

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question