问题
Up to this point I've been able to correctly view the output of my C codes by using the debugging command in Visual C++. However, when the script relies on parameters in the main function (eg/ argc
, argv
), the debugger seems to ignore both parameters and treat them as though they are uninitialized.
For instance, in the following code, the output is always printf("Usage: find pattern\n");
.
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line, int max);
/* find: print lines that match pattern from 1st arg */
main(int argc, char *argv[])
{
char line[MAXLINE];
int found = 0;
if (argc != 2)
printf("Usage: find pattern\n");
else
while (getline(line, MAXLINE) > 0)
if (strstr(line, argv[1]) != NULL) {
printf("%s", line);
found++;
}
system("Pause");
return found;
}
int getline(char *s, int lim)
{
int c;
char *i = s;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
*s++ = c;
if (c == '\n')
*s++ = c;
*s = '\0';
return s-i;
}
How can I run the code such that argc and argv get used? Should I perhaps be using an IDE other than Visual C++?
来源:https://stackoverflow.com/questions/17257104/arguments-in-main-ignored-when-debugging-in-visual-c