Concatenation in C

Asked

Viewed 391 times

5

I’m doing a C algorithm to shut down 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 the line system("shutdown -m 192.168.2.200 -s -f \n"); I need to replace the ip with mine vetor[i], but the system command only accepts one parameter.

How can I make that concatenation?

Guys. I tested the options that passed 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);
}
  • 1

    consider marking one of the answers as accepted.

  • 1

    You did ua second question that has nothing in common with the theme of the first. Please put it as a separate question, and mark an accepted answer to the original question.

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

It depends on how you will mask inaccessible Ips, assuming you leave 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);
    }
}
  • I just don’t know about the variable size str, I didn’t count the characters but it seemed little

  • It worked. Thank you!

4

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

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

Additionally, you can also use a pointer array for char char*[] without specifying the dimensions instead of a two-dimensional character array char[2][15], this avoids magic numbers by code, for example:

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

The amount of Ips contained in the above pointer vector 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;
}

3

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

srtcat example [as a complement, tip from Jefferson Quesado]

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 string_destination variable 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);

1

I did some research 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: http://www.unicamp.br/fea/ortega/info/aula09.htm http://www.cplusplus.com/reference/cstring/strcat/ -> there’s a very simple example for this command that I used.

  • I think you’ve changed strcpy with strcat, No? Only the first use of the copy is right

  • I corrected there, thank you

  • Now the first strcat is wrong. In the second loop it will put something like "shutdown -m192.168.2.200 -s -fshutdown -m" Taking advantage, there’s a gap after the -m, I believe

  • I put the space after m and reset it to the string at the end of the loop

  • It worked. Thank you!

0

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;
}
  • strcpy copies one string to another, does not take three arguments. There is also no need for conio, an obsolete library, nor need for system("pause")

  • It worked. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.