I have a question regarding passing a variable that is a char array from one function into the next.
Here are the samples of code involved:
int main( int argc, char** argv )
{
int value = 0;
int nCounter = 0;
FILE* fIn = NULL;
char * sLine = new char[MAX_FILENAME_SIZE];
char * sFileName = new char [MAX_FILENAME_SIZE];
char * s = new char [MAX_FILENAME_SIZE];
if ((fIn = fopen(ImgListFileName,"rt"))==NULL)
{
printf("Failed to open file: %s\n",ImgListFileName);
return nCounter;
}
while(!feof(fIn)){
//set the variables to 0
memset(sLine,0,MAX_FILENAME_SIZE*sizeof(char));
memset(sFileName,0,MAX_FILENAME_SIZE*sizeof(char));
memset(s,0,MAX_FILENAME_SIZE*sizeof(char));
//read one line (one image filename)
//sLine will contain one line from the text file
fgets(sLine,MAX_FILENAME_SIZE,fIn);
//copy the filename into variable s
strncpy(s, sLine, strlen(sLine)-1);
//put a \0 character at the end of the filename
strcat(sLine,"\0");
//create the filename
strcat(sFileName,s);
nCounter++;
fclose(fIn);
delete sLine;
delete sFileName;
delete s;
const int size = 60;
char path[size] = "path";
strcat(path,sFileName);
printf (path);
IplImage *img = cvLoadImage(path);
detect_and_draw(img);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("result");
void detect_and_draw( IplImage* img )
{
More code that isn't involved....
cvSaveImage(sFileName, img);
Now, I have tried the following:
void getFilename(char * sFileName)
{
printf("The filename is %s\n", sFileName);
return;
}
And then call with
char * S ="string"
getFilename(S);
cvSaveImage(S,img);
But “string” is placed into “The filename is: string”.
What can I do so that I can use sFileName, the char array, in cvSaveImage(sFileName, img)?
Thanks in advance, and if you need any further clarifications, please ask!
If I’m understanding correctly, what you have is a scoping problem. You essentially have:
The problem is that
sFileNameis local tomain()and not accessible indetect_and_draw(). You can either modifydetect_and_draw()to take a second argument:Or make sFileName a global variable declared/defined outside the scope of
main()– although that’s quite often considered to be an inferior solution.