Simple Program With one argument

Jun 1, 2011 at 8:24pm
Hi, I am learning c++. I believe I have an easy to answer question.
I have an opencv program which simply takes a video and shows it. It takes the video as an argument with argv[1]. It compiles without an error. I can successfully run the program in the command line:

C: prog.exe test.avi // it runs!

prog.exe and test.avi are in the same directory.

But when I click on the prog.exe Windows 7 OS tells that it stopped working and prog.exe stops. I believe that it has to do with taking argument as an input. What should I do if I want to run the prog.exe by clicking in order for it to take test.avi as an argument?

I am using code::blocks as an editor btw.

I don't know whether you need it but here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "highgui.h"

int main( int argc, char** argv )
{
    cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
    CvCapture* capture = cvCreateFileCapture( argv[1] );
    /*
    Takes a video as an argument and initializes it to the beginning of the video.
    */
    IplImage* frame;

    while(1) {
        frame = cvQueryFrame( capture );

        /*
        Takes frames from the video. And iterates the CvCapture structure one frame forward.
        cvLoadImage allocates some memory, but this function uses memory already allocated by cvCreateFileCapture
        */

        if( !frame ) break;
        cvShowImage( "Example2", frame );
        /*

        */
        char c = cvWaitKey(33);
        /*
        Waits for a keystroke for 33 ms, if a key is pressed during that time, the ASCII value of the key is passed to 'c'
        */

        if( c == 27 ) break;
    }

    cvReleaseCapture( &capture );
    /*
    Free all the memory associated with the CvCapture data structure.
    */
    cvDestroyWindow( "Example2" );

}

Jun 1, 2011 at 8:44pm
If you're gonna use an argument, you have to first check if it was actually passed (using argc).
You can open a program with a file as a parameter by dragging the file onto your program icon.
Jun 1, 2011 at 8:55pm
Also, you are selecting argv[1] which is the second element in the array. argv[0] yields arg #1.

Your application is probably throwing a array index out of bounds exception, however you have no error handler.
Last edited on Jun 1, 2011 at 8:56pm
Jun 1, 2011 at 8:56pm
The first argument is always the program's name, though.
Jun 1, 2011 at 9:24pm
Hey Athar,
Thanks for that quick and easy solution. I dragged the video file on prog.exe file and it ran! :)
Topic archived. No new replies allowed.