Concatenation in C

4

I'm doing an algorithm in C to turn off some machines. This is my algorithm:

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

main() {

    char vetor[2][15] = {"192.168.2.200", "192.168.2.201"};

    for(int i = 0; i < 2; i++){
        system("shutdown -m 192.168.2.200 -s -f");
    }

    system("pause");
    return 0;
}

But on line system("shutdown -m 192.168.2.200 -s -f \n"); I need to replace ip with my vetor[i] , but the system command only accepts one parameter.

How can I do this concatenation?

Galera. I have tested the options that have happened to me and it worked. Thank you.

But I have one last question.

If you have 3 IPs in my array

And one of them is inaccessible, can you do a check before running the system? For example:

for(int i = 0; i < 3; i++){

    if(vetor[i] == "inacessivel") { //Aqui poderia ser um booleano também, ou int. Coloquei para exemplificar o que desejo fazer
        continue;
}

sprintf(str, "shutdown -m %s -s -f", vetor[i]);
system(str);
}
    
asked by anonymous 19.01.2018 / 12:41

5 answers

6

Use the sprintf function,

char str[50];
char vetor[2][15] = {"192.168.2.200", "192.168.2.201"};

for(int i = 0; i < 2; i++)
{
    sprintf(str, "shutdown -m %s -s -f", vetor[i]); 
    system(str);
}

To learn more about the function, read here

Edit

Depends on how you will mask inaccessible IPs, assuming you leave it as empty text could look like this:

for (int i = 0; i < 2 /*Tamanho vetor*/; i++)
{
    if (strcmp(vetor[i], "") != 0)
    {
        sprintf(str, "shutdown -m %s -s -f", vetor[i]);
        system(str);
    }
}
    
19.01.2018 / 13:00
4

In C , you can use the sprintf() function of the default stdio.h library to "mount" your command using format specifier %s in a chars buffer, eg:

char buf[256] = {0}
sprintf( buf, "shutdown -m %s -s -f", ips[i] );
system(buf)

In addition, you can also use a vector of pointers to char char*[] without specifying the dimensions in place of an array of two-dimensional chars char[2][15] , this prevents #

char * ips[] = { "192.168.2.200", "192.168.2.201", "192.168.2.100", "192.168.2.105" };

The number of IPs contained in the vector of pointers above can be calculated as follows:

int n = sizeof(ips) / sizeof(ips[0]);

Putting it all together:

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

#define CMD_MAX_TAM     (512)

/* Lista de IPs */
char * ips[] = { "192.168.2.200", "192.168.2.201", "192.168.2.100", "192.168.2.105" };

int main( void )
{
    int i = 0;

    /* Buffer para montagem do comando */
    char cmd[CMD_MAX_TAM+1] = {0};

    /* Calcula quantidade de IPs contidos no vetor */
    int n = sizeof(ips) / sizeof(ips[0]);

    /* Para cada IP do vetor */
    for( i = 0; i < n; i++ )
    {
        /* Formata o comando a ser executado */
        sprintf( cmd, "shutdown -m %s -s -f", ips[i] );

        /* Executa comando */
        system( cmd );
    }

    system("pause");

    return 0;
}
    
19.01.2018 / 13:26
2

You have strcat that allows you to concatenate two strings or the sprintf to format the string with the variables you want.

example srtcat [as a complement, Jefferson Quesado tip]

char str_destino[80] = "shutdown -m ";
char ip[100] = "192.168.1.15";
strcat (str_destino, ip);
strcat (str_destino," -s -f");

system(str_destino);
//puts (str_destino);

Sprintf is more versatile and allows you to do everything in one line of code.

sprintf(string_destino, "shutdown -m %s -s -f", string_origem);

The variable string_destino will save the formatting you indicate in sprintf, then just use it when needed.

char string_destino[31];

sprintf(string_destino, "shutdown -m %s -s -f", "127.0.0.1"); 

system(string_destino);
    
19.01.2018 / 13:09
1

I researched a little and thought about it:

string_desliga[31] = "";
char vetor[2][15] = {"192.168.2.200", "192.168.2.201"};    

for(int i = 0; i < 2; i++){
        strcat (string_desliga,"shutdown -m ");
        strcat (string_desliga,vetor[i]);
        strcat (string_desliga," -s -f");
        system(string_desliga);
        strcpy(string_desliga,"");
}

References: link link - > has a very simple example for this command that I used.

    
19.01.2018 / 13:10
-1

try,

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

main() {
  char vetor[2][15] = {"192.168.2.200", "192.168.2.201"};
  char linhacomando[50];

for(int i = 0; i < 2; i++){
  strcpy(linhacomando, "shutdown -m %s -s -f", vetor[i]);
  system(linhacomando);
}

 system("pause");
return 0;
}
    
19.01.2018 / 13:09