Home > Enterprise >  How to insert parameters to command prompt when starting a program in c ?
How to insert parameters to command prompt when starting a program in c ?

Time:04-20

I have no idea how to work with command prompt. I don't even know which parameters I should write. Here's the code we were given:


//random example function
void KMP(const string& text, const string& sample) {
    ofstream file("iz.txt");
    file<<"Test: "<<sample;
    file.close();

}

int main(int argc, const char *const argv[]) {
    if (argc != 3) {
        return -1;
    }

    string text = inputText(argv[2]);
    string sample = argv[1];
    out.open("out.txt");

    if (!out) {
        return -2;
    }

    KMP(text, sample); //KMP algorithm function that I finished coding in my program (it would be too long to copy-paste all here), but I don't know how to call it in command prompt.
    return 0;
}

How can I start the program with command prompt

CodePudding user response:

The passing of command line arguments is not specified by the C language. It is specified by the shell of the operating system that you are using. Quite commonly, program's execution follows this pattern:

./path/to/executable argument1 argument2

CodePudding user response:

First, you need to compile the program.

In command prompt-

Go to the respective path. Now,

g   /yourfilename.cpp/ -o main

Now to run the file,

./main parameter1 parameter2
  • Related