Solved

Linux Red hat 9 - C language

Posted on 2003-10-26
6
199 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now