簡體   English   中英

C ++如何將變量char數組從一個函數傳遞給另一個函數

[英]C++ How to pass a variable char array from one function to another

我有一個問題,關於將一個字符數組的變量從一個函數傳遞到下一個函數。

以下是涉及的代碼示例:

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)中使用char數組sFileName?

在此先感謝您,如果您需要任何進一步的說明,請詢問!

忽略未定義的行為,不必要的動態分配等,您似乎想完成的事情歸結為以下一般順序:

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實際上尖叫“此代碼忽略了單一責任原則”。 它在名稱中列出了兩個職責,並且似乎也至少有三分之一(保存文件)。

如果我的理解正確,那么您遇到的是一個范圍界定問題。 您基本上有:

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_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()范圍之外聲明/定義的全局變量-盡管通常將其視為劣等解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM