By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Problem- strcat with char and char indexed from char array

P: n/a
Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr[i];
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends

Apr 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<al*********@gmail.com> skrev i meddelandet
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr[i];
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends


Hello,

A suggestion: Change the prolematic code into something like the following;

#include <sstream>
#include <iostreama>
//.... Your beginning code here...
std::ostringstream o;
o<<"output"<<arr[i]<<".jpg";
std::cout<<o.str()<<std::endl;
cvSaveImage(o.str().c_str(),region);
// .... Your ending code here...
Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Apr 20 '06 #2

P: n/a

<al*********@gmail.com> skrev i meddelandet
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr[i];
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends

Hello again,

Just a follow-up; strcat expects two char* arguments. One of the arguments
can't be just char.

Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Apr 20 '06 #3

P: n/a
Champion!

Works like a dream.

Thanks Peter.

Cheers, Al.

Apr 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.