C++ C++;如何将变量字符数组从一个函数传递到另一个函数
我有一个关于将字符数组变量从一个函数传递到下一个函数的问题 以下是所涉及的代码示例:C++ C++;如何将变量字符数组从一个函数传递到另一个函数,c++,variables,arrays,C++,Variables,Arrays,我有一个关于将字符数组变量从一个函数传递到下一个函数的问题 以下是所涉及的代码示例: 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_
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);
现在,我尝试了以下方法:
void getFilename(char * sFileName)
{
printf("The filename is %s\n", sFileName);
return;
}
然后打电话给我
char * S ="string"
getFilename(S);
cvSaveImage(S,img);
但是“字符串”被放在“文件名是:字符串”中
我该怎么做才能在cvSaveImage(sFileName,img)中使用sFileName,char数组
提前感谢,如果您需要任何进一步的澄清,请询问 如果我理解正确,您遇到的是范围界定问题。你基本上有:
int main(/* */)
{ char sFileName[MAX_FILENAME_SIZE];
/* code to initialize sFileName to contain a value */
detect_and_draw(img);
}
void detect_and_draw(IplImage *img)
{ cvSaveImage(sFileName, img);
}
问题在于sFileName
是main()
的本地文件,在detect\u and\u draw()
中无法访问。您可以修改detect\u和\u draw()
以获取第二个参数:
int main()
{ /* stuff */
detect_and_draw(img, sFileName);
}
void detect_and_draw(IplImage *img, const char* fn)
{ cvSaveImage(fn, img);
}
或者使sFileName成为在
main()
范围之外声明/定义的全局变量-尽管这通常被认为是一个较差的解决方案。如果我理解正确,您遇到的是一个范围问题。你基本上有:
int main(/* */)
{ char sFileName[MAX_FILENAME_SIZE];
/* code to initialize sFileName to contain a value */
detect_and_draw(img);
}
void detect_and_draw(IplImage *img)
{ cvSaveImage(sFileName, img);
}
问题在于sFileName
是main()
的本地文件,在detect\u and\u draw()
中无法访问。您可以修改detect\u和\u draw()
以获取第二个参数:
int main()
{ /* stuff */
detect_and_draw(img, sFileName);
}
void detect_and_draw(IplImage *img, const char* fn)
{ cvSaveImage(fn, img);
}
或者将sFileName作为一个在
main()
范围之外声明/定义的全局变量-尽管这通常被认为是一个低级的解决方案。忽略未定义的行为、不必要的动态分配等,您似乎要完成的事情归结为以下一般顺序:
std::string path;
while (std::getline(fIn, path)) {
std::cout << "path: " << path;
IplImage *img = cvLoadImage(path.c_str());
detect_and_draw(img, path);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("result");
}
void detect_and_draw(IpImage *img, std::string const &path) {
// ...
cvSaveImage(path.c_str(), img);
}
使用该选项,您的代码看起来更像这样:
while (std::getline(fIn, path)) {
Image img(path);
img.detect_and_draw();
cvWaitKey();
cvDestroyWindow("result");
}
这还不完全清楚,但是cvDestroyWindow
听起来也很像是真正属于析构函数的东西,但我不太确定这些片段如何组合在一起,以确定是什么析构函数——可能是Image
,更可能是其他东西
我注意到,
detect\u and\u draw
几乎尖叫着“这个代码忽略了单一责任原则”。它在名称中列出了两项职责,并且似乎至少还有第三项职责(保存文件)。忽略未定义的行为、不必要的动态分配等,您似乎要完成的工作归结为以下一般顺序:
std::string path;
while (std::getline(fIn, path)) {
std::cout << "path: " << path;
IplImage *img = cvLoadImage(path.c_str());
detect_and_draw(img, path);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("result");
}
void detect_and_draw(IpImage *img, std::string const &path) {
// ...
cvSaveImage(path.c_str(), img);
}
使用该选项,您的代码看起来更像这样:
while (std::getline(fIn, path)) {
Image img(path);
img.detect_and_draw();
cvWaitKey();
cvDestroyWindow("result");
}
这还不完全清楚,但是cvDestroyWindow
听起来也很像是真正属于析构函数的东西,但我不太确定这些片段如何组合在一起,以确定是什么析构函数——可能是Image
,更可能是其他东西
我注意到,
detect\u and\u draw
几乎尖叫着“这个代码忽略了单一责任原则”。它在名称中列出了两个职责,并且似乎至少还有第三个职责(保存文件)。char*S=“string”
应该是const char*S..
。字符串文字是只读内存。更好的是,std::string
。您应该使用delete[]
来释放数组(sLine
,sFileName
,s
)。@Michael,然后更改getFilename
)。如果你不能做到这一点,并且你确信它不会改变任何事情,const\u cast
。否则,将其复制到一个新数组中。@Michael:使用std::string
,当cvSaveImage
需要char const*
时,使用字符串的.c_str()
成员函数来获取它。可以相当公平地猜测删除sFileName;[…]strcat(路径,sFileName)
可能是部分问题的原因。char*S=“string”
应该是const char*S..
。字符串文字是只读内存。更好的是,std::string
。您应该使用delete[]
来释放数组(sLine
,sFileName
,s
)。@Michael,然后更改getFilename
)。如果你不能做到这一点,并且你确信它不会改变任何事情,const\u cast
。否则,将其复制到一个新数组中。@Michael:使用std::string
,当cvSaveImage
需要char const*
时,使用字符串的.c_str()
成员函数来获取它。可以相当公平地猜测删除sFileName;[…]strcat(路径,sFileName)代码>可能是部分问题的原因。它似乎不允许我给出detect\u和\u draw两个参数。这是怎么回事?你有什么错误?在我的脑海中,可能有一个头文件,其中还包含detect\u和
的声明,您还需要修改该声明以指定附加参数。实际上,它确实允许这样做。我没有意识到这一点,但我有一点旧代码,我甚至没有注意到,限制参数只有1。否则,您的解决方案将非常有效。谢谢它似乎不允许我给detect_和draw两个参数。怎么会这样?你有什么错误?在我的脑海中,可能有一个头文件,其中还包含detect\u和
的声明,您还需要修改该声明以指定附加参数。实际上,它确实允许这样做。我没有意识到这一点,但我有一点旧代码,我甚至没有注意到,限制参数只有1。否则,您的解决方案将非常有效。谢谢我喜欢这种方法,而且很有意义。至于detect_和_draw,我一定会改变这一点……我喜欢这种方法,而且很有意义。至于探测和绘制,我一定会改变。。。