1

I have a C++ Program:

#include <iostream>
using namespace std;
int main (){
    char x [10] = "abc";
    system (sprintf ("mkdir ", "%s", x));    //error happens here, can't 
                                             //convert int to const char*
    return 0;
}

results in:

sys.c:8:37: error: invalid conversion from ‘int’ to ‘const char*’

Where am I converting an int to a char?

Deanie
  • 2,316
  • 2
  • 19
  • 35
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 4
    Look up the documentation on `sprintf`. – chris Jan 10 '13 at 09:10
  • 1
    @chris I looked here: http://www.cplusplus.com/reference/cstdio/sprintf/ – Aquarius_Girl Jan 10 '13 at 09:10
  • @chris HUH, I realize now, I had NOT read that page "carefully"! `str - Pointer to a buffer where the resulting C-string is stored.` – Aquarius_Girl Jan 10 '13 at 09:18
  • 3
    Besides the answers you got to your question, it seems that your are compiling this as C++ but your question is tagged C. Generally it is not a good idea do mix these like that, there are subtle differences between the two languages that *will* bite you one day or another. – Jens Gustedt Jan 10 '13 at 09:18
  • @JensGustedt yes, I know, but sprintf is a part of C, hence the tag. :) – Aquarius_Girl Jan 10 '13 at 09:19
  • 1
    @AnishaKaul I you want to use sprintf in C++, you should include instead of , to make the difference clear. Also, since sprintf is de-facto part of C++, and you use C++, the question should be tagged C++ instead of C. Also, I doubt that you need in the code you posted, and if you do, use instead. – Andreas Grapentin Jan 10 '13 at 09:33
  • sprintf Upon successful return, these functions return the number of characters printed so its int U are trying – Rashmi A M Jan 10 '13 at 09:34
  • @AndreasGrapentin Thanks for the help. Please edit the question as you like it. – Aquarius_Girl Jan 10 '13 at 09:34
  • @AnishaKaul I've just retagged it. I won't change the code because of possible references :) Also, regarding `system` you should read http://www.cplusplus.com/forum/articles/11153/ – Andreas Grapentin Jan 10 '13 at 09:36
  • @AndreasGrapentin I have now removed the traces of c++ from the question making it solely focus on sprintf. Is it okay now? – Aquarius_Girl Jan 10 '13 at 09:36
  • @AndreasGrapentin thanks for the link. but, I reverted it back to full C. – Aquarius_Girl Jan 10 '13 at 09:36
  • @AnishaKaul wait, are we talking C or C++ now? – Andreas Grapentin Jan 10 '13 at 09:37
  • @AndreasGrapentin Well, about sprintf in C. :) Doesn't sprintf behave same way in C and c++? – Aquarius_Girl Jan 10 '13 at 09:42
  • @AnishaKaul I was asking because the errors you got initially showed that you compiled the code as C++ :) also, see my answer below for more information on the snprintf family – Andreas Grapentin Jan 10 '13 at 09:47
  • 3
    Apart from obvious c/c++ confusion, I can't see any reason to close this question as "too localized". Very surprised with the close votes on a well asked question. – Krishnabhadra Jan 10 '13 at 10:22

3 Answers3

3

system expects a const char * as input, whereas sprintf returns an int. This is the problem which is leading your error. Also, the first argument of sprintf must be a writable buffer of sufficient size.

Jay
  • 24,173
  • 25
  • 93
  • 141
3

I know that the question has been answered already, but I wanted to share another approach to this, and it's too verbose for a comment :)

short example:

#include <cstdio>
#include <cstdlib>

int main()
{
    char x[] = "yourdirectory";
    /* len will contain the length of the string that would have been printed by snprintf */
    int len = snprintf(0, 0, "mkdir %s", x);

    /* casting the result of calloc to (char*) because we are in C++ */
    char *buf = (char*)calloc(len + 1, sizeof(char)); 
    /* fail if the allocation failed */
    if(!buf)
        return -1;

    /* copy to buffer */
    snprintf(buf, len + 1, "mkdir %s", x);

    /* system is evil :( */
    system(buf);

    return 0;
}

since snprintf returns the number of characters that would have been printed, giving it a length of 0 will tell you how large a buffer you have to allocate for the command.

this code will also work with arbitrary directories given by a user and will not suffer from input truncation of buffer overruns.

Take care :)

EDIT: of course, system is still evil and should never be used in "real" code ;)

Andreas Grapentin
  • 5,499
  • 4
  • 39
  • 57
1

From sprintf documentation

sprintf

int sprintf ( char * str, const char * format, ... );

sprintf() write formatted data to string.

Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.

str - Pointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string.

sprintf writes to the first argument, and in your case the first argument is a string literal .

Also as other answer points out, sprintf returns int and system() expects const char*

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167