0

Possible Duplicate:
C++ convert int and string to char*

The function I use looks like this:

int cvSaveImage(const char* filename,const CvArr* image);

This function expects a const char* as first parameter. For example:

cvSaveImage("ImageName",img)

would be right.

However, I would like to put instead of 'Name' in "ImageName", an int variable value. I've tried something like this, but doesn't work at all, it crashes:

int num = 10;
char buffer[1024];
sprintf(buffer,"Image%d",num);
cvSaveImage((const char *)buffer,img);

Any ideas?

Community
  • 1
  • 1
allissaid
  • 15
  • 4
  • 2
    That looks about right (modulo a few typos). You also ought not need to cast the buffer - if you remove the cast, do you get a compile error (i.e. have you got the types wrong)? Which line do you get the crash on? What's the call stack? – Rup Jul 06 '12 at 10:26
  • 4
    [Documentation](http://opencv.willowgarage.com/documentation/c/reading_and_writing_images_and_video.html#saveimage) suggests that file type is determined by extension. Does it crash if you do something like `Image%d.jpg` instead? – Vlad Jul 06 '12 at 10:27
  • What is img? Maybe the problem is there? – Kirill Kobelev Jul 06 '12 at 10:28
  • what about itoa? http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ – Arsen Mkrtchyan Jul 06 '12 at 10:28
  • You do not need a cast when passing a non-constant pointer or an array to a function that expects a `const` pointer. – Sergey Kalinichenko Jul 06 '12 at 10:30
  • The code looks ok. Try to print out the name in `buffer`. Also, check if `cvSameImage` stores a pointer to the buffer which it use later. If that is the case, you must place the buffer in a location that lives long enough. – Lindydancer Jul 06 '12 at 10:31
  • 2
    Use `sprintf(buffer,"Image%d.jpg\0",num);`, or something like that (with appropriate extention and the terminating zero – SingerOfTheFall Jul 06 '12 at 10:34
  • I don't know about the cause of the crash, but the question, as it is, is a duplicate. http://stackoverflow.com/questions/1766150/c-convert-int-and-string-to-char. Also related: http://stackoverflow.com/questions/7934907/convert-int-to-const-char-in-order-to-write-on-file, http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c – jogojapan Jul 06 '12 at 10:36
  • 4
    @SingerOfTheFall The `\0` is superfluous. A `'\0'` is appended to all string literals. – James Kanze Jul 06 '12 at 10:41
  • @ArsenMkrt Note that itoa isn't standard C nor standard C++. – Lundin Jul 06 '12 at 10:42

2 Answers2

2

You must specify an extension for the file. That is the only way openCV firgures out the format to write in. OpenCV throws an exception which is most likely why your program is crashing.

Also, if you are working on a new project, please consider moving to the C++ interface of openCV. It is much more structured, has more features and is also recommended.

go4sri
  • 1,490
  • 2
  • 15
  • 29
0

If you are using strings, this function should do your trick:

#include <sstream>
#include <string>

std::string IntToString(int N)
{
    ostringstream ss("");
    ss << N;
    return ss.str();
}
physicalattraction
  • 6,485
  • 10
  • 63
  • 122