C ++如何将变量char数组从一个函数传递给另一个函数

时间:2012-07-24 19:54:58

标签: c++ variables arrays

我有一个关于将一个char数组的变量从一个函数传递到下一个函数的问题。

以下是涉及的代码示例:

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);

但是“string”被放入“文件名是:string”。

我能做些什么才能在cvSaveImage(sFileName,img)中使用sFileName,char数组?

提前致谢,如果您需要进一步澄清,请询问!

2 个答案:

答案 0 :(得分:2)

忽略未定义的行为,不必要的动态分配等,您似乎想要完成的事情可以归结为这个一般顺序:

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);
}

我认为我做的事情与此有点不同 - 可能从Image类开始,类似于:

class Image { 
    IpImage *img;
    std::string path;

public:
    Image(std::string const &name) : 
        img(cvLoadImage(name.c_str()), path(name) 
    { }

    ~Image() { cvReleaseImage(&img); }

    void detect_and_draw() { 
         // ...
         cvSaveImage(path);
    }
};

使用它,您的代码看起来更像是这样:

while (std::getline(fIn, path)) {
    Image img(path);
    img.detect_and_draw();
    cvWaitKey();
    cvDestroyWindow("result");
}

它并不完全清楚,但cvDestroyWindow听起来很像真正属于析构函数的东西,但我不确定这些碎片是如何组合在一起的,以确保什么析构函数 - 也许是Image,更可能是其他东西。

我注意到detect_and_draw实际上是尖叫声&#34;这段代码忽略了单一责任原则&#34;。它列出了名称中的两个职责,并且似乎至少有三分之一(保存文件)。

答案 1 :(得分:1)

如果我理解正确,你所拥有的是一个范围问题。你基本上有:

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);
}

问题在于sFileNamemain()的本地,而detect_and_draw()无法访问。您可以修改detect_and_draw()以获取第二个参数:

int main()
{ /* stuff */
  detect_and_draw(img, sFileName);
}
void detect_and_draw(IplImage *img, const char* fn)
{ cvSaveImage(fn, img);
}

或者使sFileName成为在main()范围之外声明/定义的全局变量 - 尽管这通常被认为是一个较差的解决方案。