开发者

Passing a filename through a command-line argument vs. using a string literal in-code

The following bit of code seems to interp开发者_Go百科ret the slash in the passed filename differently, depending on whether it was passed as a command line argument, or hardcoded by means of a literal. If the image filename C:\kimba.jpg is passed in as a command line argument, it works fine:

int main( int argc, char** argv ) 
{
    IplImage* img = cvLoadImage(argv[1]);
    //IplImage* img = cvLoadImage("C:\kimba.jpg", 1);

    // ...

    return 0;
}

If, on the other hand, I uncomment the second line and comment the first, the same filename causes an exception in that line. I do not know how cvLoadImage() is implemented, but it seems (according to the debugger) that in both cases the same content is being passed to the function. So why does the hardcoded filename cause an exception and not the command-line argument?


You need to escape the backslash with a backslash:

IplImage* img = cvLoadImage("C:\\kimba.jpg", 1);


the '\' character is the escape character in C++. To get a '\' character literal, you need to escape it (note the double '\'):

IplImage* img = cvLoadImage("C:\\kimba.jpg", 1);

HTH.


Use "C:\\kimba.jpg" - \ is used to escape special characters such as \n


Backslash in string literals is an escape character, while in command line argument it is interpreted as-is. So in the second case the file is not found and you get an exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜