jet
jet

Reputation: 183

Command Line Parameters

I'm trying to add my file in Visual Studio as command line parameters. I know my code works since when I use fopen("whole path here", "r"), it runs. I then add the file as a command line parameter instead, and I get no such file or directory. Any thoughts? Thanks.

Upvotes: 3

Views: 2371

Answers (3)

wallyk
wallyk

Reputation: 57784

Are you sure the command line parameter is handled correctly? Temporarily replace your main() with this:

int
main (int argc, char **argv)
{
    int j;
    for (j = 0;  j < argc;  ++j)
        printf ("argv [%d] = '%s'\n", j, argv [j]);
    return 0;
}

My guess is that you have file paths with spaces in them. Those have to be quoted on the command line:

C:\> myprogram "c:\Documents and Settings\Administrator\My Documents\Test.dat"

If this were unquoted, the test program would output:

argv [0] = 'myprogram.exe'
argv [1] = 'c:\Documents'
argv [2] = 'and'
argv [3] = 'Settings\Administrator\My'
argv [4] = 'Documents\Test.dat'

Upvotes: 1

Priyank Bolia
Priyank Bolia

Reputation: 14450

You can always debug in Visual Studio what file name you are getting from the command line, and then you have an idea of what's wrong.

Upvotes: 0

Konamiman
Konamiman

Reputation: 50313

Has your file path spaces? If so, you need to enclose it in quotes.

Upvotes: 4

Related Questions