3

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++?

ericgrosse
  • 1,490
  • 20
  • 37
  • 3
    In your project properties, you can specify command-line arguments to start your process with. The exact instructions might depend on which version of Visual Studio you're using. – jamesdlin Jun 23 '13 at 02:24
  • Yes definitely, you should quit using Visual C++,its an absolute crap. – 0decimal0 Jun 23 '13 at 05:23
  • What IDE would you recommend? – ericgrosse Jun 23 '13 at 20:10
  • I do not agree with PHlfounder. There is nothing wrong with the VS IDE. It's not as if this particular problem is specific to VS; in *any* IDE that launches a process for you, at some point you're going to need to specify somewhere what you want your command-line arguments to be. – jamesdlin Jun 23 '13 at 20:20
  • Damn, I'm seeing the same problem in VS 2013. I guess command-line debugging just isn't that important anymore. – SilverbackNet Jul 13 '15 at 08:03

0 Answers0