systemio
systemio

Reputation: 145

How to string concat and calling System with the char var

im trying to concatsomm strings and call a expext script on my server, but im a .net programer and new to c and pointers so keep messing up... what am i doing wrong here?

or a better question wold be how should i really do this?

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

int main(int argc, char *argv[])
{
    //location of expect script that ssh and gets info on other server
    char str0[] = "./home/systemio/Develop/getfile ";

    char* strP =  argv[1];
    char str1 = (char)*strP;
    char str2[] = " > file.txt";

    strcat(str0,str1);

    strcat(str0,str2);

    printf("%s\n", str0);
    system(str0);

    printf("Done!!!!\n");
    return 0;
}

Upvotes: 2

Views: 1260

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409216

This line will not work:

strcat(str0,str1);

That is because str1 is not a string! It is a single char. Strings can only be char-pointers or char-arrays.

And as noted by others, str0 is not big enough, so you will overwrite memory which will cause undefined behavior.

If I may give an alternate solution to what you are trying to do:

char str[100];

sprintf(str, "./home/systemio/Develop/getfile %c > file.txt", argv[1][0]);
printf("%s\n", str);
system(str);

Edit: Explanation of why I use argv[1][0]

The reason is because of these two lines in the question:

char* strP =  argv[1];
char str1 = (char)*strP;

These two lines get the first character from argv[1], in an indirect way. If you want the whole of argv[1] then my sprintf will look like this instead:

sprintf(str, "./home/systemio/Develop/getfile %s > file.txt", argv[1]);

Upvotes: 4

hmjd
hmjd

Reputation: 122001

You can allocate a buffer for the system command to ensure there is definitely enough space to construct the full command:

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

int main(int argc, char* argv[])
{
    /* Ensure 1 argument supplied. */
    if (2 == argc)
    {
        /* malloc()ing the system command buffer means you can safely
           edit 'util' and 'output' without being concerned about the
           size of an array.
           The '+2' is for the char from argv[1]
           and for the terminating null character. */
        const char* util   = "./home/systemio/Develop/getfile ";
        const char* output = " > file.txt";
        char* str0         = malloc(strlen(util) + strlen(output) + 2);

        if (str0)
        {
            if (sprintf(str0, "%s%c%s", util, *argv[1], output) > 0)
            {
                printf("%s\n", str0);
                system(str0);
                printf("Done!!!!\n");
            }

            /* free() malloced memory. */
            free(str0);
        }
    }

    return 0;
}

Upvotes: 2

Adrian
Adrian

Reputation: 5681

Concatenation in C is not like it is done in Java or C#. (You can't do "A" + "B" and get "AB")

Read: http://cplusplus.com/reference/clibrary/cstring/strcat/

strcat(dest,src)

You need to reserve space in destination to make sure the appended string fits inside the destination variable. (must have first "A " then copy "B").

I prefer strcpy

Upvotes: 1

Related Questions