C prgramming (not C++)
## Requirements
You are making an entire program
All this program does is print the environment variables to a file
called "env.txt"
Each variable is followed by a newline character
example:
inputs:
envp/environ = {"E1=2","E2=7",NULL}
outputs:
env.txt as a string would be
"E1=2\nE2=7\n"
example:
inputs:
envp/environ = {NULL} or NULL
outputs:
env.txt as a string would be ""
#include <stdio.h>
extern char **environ;
int main(void)
{
int i;
FILE * fp;
fp = fopen ("env.txt","w");
for (i = 0; environ[i] != NULL; i++){
if(environ[i]==NULL){
fp = fopen ("env.txt","w");
fprintf (fp, "");
}
fprintf (fp, "%s\n",environ[i]);
}
fclose (fp);
return 0;
}
you can also get at the environment via environ, even in functions
other than main(). The variable environ is unique amongst the
global variables defined by POSIX ans is not declared in any header
file, so you must write the declaration yourself.
-----------------------------------------
Please give me a UPVOTE. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.