Solved

Linux Red hat 9 - C language

Posted on 2003-10-26
6
203 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.

815 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

11 Experts available now in Live!

Get 1:1 Help Now