?
Solved

Linux Red hat 9 - C language

Posted on 2003-10-26
6
Medium Priority
?
213 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 600 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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

750 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