Format the output of System () in C

1

I simply have an ascci art that I show with different printfs and I want to put in the middle the name of the user. (with the name I mean the windows user).

I thought about taking out the name with a System ("whoami"), which works for me but because I can not store it in a variable I can not format it and when I put the system and the command to peel, automatically after put the user prints a line break.

If someone has any idea how to manipulate it or how to take out the username in a different way would be very helpful.

Summing up specifications:

Language C. (Only C not c ++ or anything like that)

Operating system (Windows)

Specific problem: The problem occurs when I try to print the user name on the screen in the middle of many characters, and with the System ("Whoami") it prints the name but makes an automatic line jump after the name.

Possible solutions thought: Clearly I do not have a solution but I thought of some way of being able to store this with something similar to a System () in a variable since with the system you can not or some way of formatting it to establish that it does not make the line break.

        printf("***********************************************************************\n");   
printf("|                                       %s             |\n",string1); //user , String1
   printf (" ---------------------------------------------------------------------\n"); 

System("whoami");

As you can see in the code I want the result of the whoami to go before the variable string1 but on the same line as the String1 since it is very important to keep the format because at the bottom there is a part of aesthetic ascci.

    
asked by WhySoBizarreCode 30.05.2018 в 04:01
source

1 answer

3

The system function returns int , and what it does is give control to another software fragment (Through the system shell) which is out of your control, same way gives you the control of stdin , stdout and stderr to show and / or request data through the same streams that your app has (Not checked, but it must be like this) .

Assign the value thrown by the command whoami called from system(const char *); It is a somewhat tedious task, but not impossible.

If what you want to do is print the value of the environment variable $USER 1 , I recommend using getenv(const char *); , this function is used to return the value of the environment variables as a text string ( char * ) that your program can use:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char *valorVariable = getenv("USER");
  if (valorVariable)
    printf("La variable de entorno $USER tiene el valor: '%s'.\n", valorVariable);
  else
    fprintf(stderr, "Error! No pude encontrar la variable de entorno $USER\n");
  return 0;
}

With this already said, you can add it to your ASCII Art:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char *currentUser = getenv("USER");
  if (currentUser) {
    printf("************************\n");
    printf("| %20s |\n", currentUser); // Esto es una prueba.
    printf("************************\n");
  }
  return 0;
}

By running the code fragment placed above I receive the following output (From an online IDE that operates with ubuntu) :

************************
|               ubuntu |
************************

From then on, positioning it in the middle is a matter of mathematics and other things.

Greetings:)

1: As far as I know, the environment variable $USER is only available in linux (and MacOS, I think) , for Windows you should use USERNAME .

    
answered by 30.05.2018 / 14:32
source