0

I am now using Visual studio code and I want to debug C/C++ program which runs as a command line. However, debug of Visual studio code just supports the normal program (not a command line program). When I want to debug a command line program, I need to edit my code in which I initialize the argument before running debugger. For example

int main(int argv, char *argc[])
{
    if (argv != 2)
    {
        printf("Wrong syntax\nCorrect Syntax: readfile <source file>\n");
        exit(1);
    }
    FILE *fp = fopen(argc[1], "rb");
}

When I want to debug, I need to initialize like this:

int main()
{
    /*if (argv != 2)
    {
        printf("Wrong syntax\nCorrect Syntax: readfile <source file>\n");
        exit(1);
    }*/ //eliminate this part
    argc[1] = "phonebook.dat";
    FILE *fp = fopen(argc[1], "rb");

I find it annoying. So how do I change the debugger on VS code to debug C and C++ command line programs?

Hoang Nam
  • 548
  • 3
  • 18
  • 1
    What is "the normal program"? And what do you need to edit? – Gerhardh Apr 04 '20 at 10:43
  • @Gerhardh I have edited it, read it again please – Hoang Nam Apr 04 '20 at 10:47
  • Does this answer your question? [Debugging with command-line parameters in Visual Studio](https://stackoverflow.com/questions/298708/debugging-with-command-line-parameters-in-visual-studio) – kaylum Apr 04 '20 at 10:48
  • What is "not a command line program"? Every program takes a command line. Whether it uses the parameters or opens a GUI or not is a different story. – Gerhardh Apr 04 '20 at 10:49
  • @kaylum Do you see that I am using Visual STUDIO CODE? – Hoang Nam Apr 04 '20 at 10:49
  • Apologies, I did miss that. But have you tried searching "pass command line arguments to visual studio code"? Such as [this](https://stackoverflow.com/questions/34382899/visual-studio-code-user-defined-argument) and [this](https://stackoverflow.com/questions/45582698/is-it-possible-to-pass-arguments-to-a-task-in-visual-studio-code) – kaylum Apr 04 '20 at 10:51
  • Did you try to add `args` to the launch configuration? For details see https://code.visualstudio.com/docs/editor/debugging – Gerhardh Apr 04 '20 at 10:52
  • @kaylum it doesn't help because it works for another language – Hoang Nam Apr 04 '20 at 10:55

1 Answers1

0

You need to edit your configuration to receive command line arguments.

See this: Configuring launch.json