Question

In: Computer Science

Write a C program which prints the environment variables to a file "sample.txt" . All variables...

Write a C program which prints the environment variables to a file "sample.txt" . All variables are followed by a newline character. example: example: inputs: envp/environ = {"E1=5","E2=9",NULL} outputs: env.txt as a string would be "E1=5\nE2=9\n".

Solutions

Expert Solution

This is the version that should work on old environments.

#include <stdio.h>
int main(int argc, char **argv, char **envp)
{
int i = 0;
FILE *fp;

fp = fopen("sample.txt", "w");
if (fp == NULL)
{
perror("File Not found");
return -1;
}
while(envp[i])
{
fprintf(fp, "%s\n", envp[i]);
i++;
}

fclose(fp);
return 0;
}

=======================

In old times there was third option to main argument that would point to list of all the environment variables just like argv points to list of command line arguments.
int main(int argc, char **argv, char **envp);

But that option is not portable in some posix systems, and works in unix like system. A better approach is to use
char **environ defined in unistd.h which holds the same list of environment variable.

Below is the program to read this variable one by one and print the variable with values to sample.txt file.

#include <stdio.h>
#include <unistd.h>
int main()
{
int i = 0;
FILE *fp;

fp = fopen("sample.txt", "w");
if (fp == NULL)
{
perror("File Not found");
return -1;
}
while(environ[i])
{
fprintf(fp, "%s\n", environ[i]);
i++;
}
fclose(fp);
return 0;
}

Compilation: gcc <filename.c>

Few lines of text from sample.txt file

OS=Windows_NT
COMMONPROGRAMFILES=C:\Program Files (x86)\Common Files
PROCESSOR_LEVEL=6
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\AutoIt3\AutoItX
PROCESSOR_ARCHITEW6432=AMD64
CUDA_PATH_V10_2=D:\Program Files\NVIDIA GPU Toolkit\CUDA\v10.2


Related Solutions

Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
Python # Write a program that examines three variables—x, y, and z # and prints the...
Python # Write a program that examines three variables—x, y, and z # and prints the largest odd number among them. # If none of them are odd, it should print a message to that effect. n = input('Enter the 1st Integer x: ') x = int(n) n = input('Enter the 2nd Integer y: ') y = int(n) n = input('Enter the 3rd Integer z: ') z = int(n) if x % 2 == 0 and y % 2 ==...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT