Reputation: 29
I am having an issue passing command line arguments to my program using Visual C++ Express 2010. I found the command arguments under debugging and using the following input, just the terms with white space between them. The file is in my project folder with the .c source code.
TestFile1.txt 2
The program works fine when I just statically define the char pointer under main. So at this point I'm not sure if the issue is with 2010 or the code. I haven't figured out a way to compile and execute in some other way to test command line args. It would be great if someone could compile this an see if it works on their system.
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 256
int main(char *argv[])
{
//char *argv[] = { "program", "TestFile1.txt", "2" };
char buf[BUFFER_SIZE];
FILE *inFp;
printf("%s",argv[1]);
if ((inFp = fopen (argv[1], "r")) == NULL)
{
fprintf(stderr, "Can't open file\n");
exit(EXIT_FAILURE);
}
fclose(inFp);
return 0;
}
Upvotes: 2
Views: 500
Reputation: 25705
it should be int main(int argc, char *argv[])
Other than that, I have not seen any other problem with your program.
Upvotes: 2