How to take command line argument in Codeblock 10.05?

浪尽此生 提交于 2019-12-17 15:39:51

问题


I am writing a C code in codeblock version 10.05.

The program is:

int main(int argc , char *argv[])
{
    printf("Entered number is %s \n", argv[1]);
    return 0;
}

However, when i compile current file, & then run the program, a terminal appears. But, the terminal doesn't wait for command line input & it directly outputs

<null>

Note that in the above program, i have omitted the code for handling zero number of command line arguments. How can i supply command line arguments?


回答1:


With code::blocks you can set your command line arguments like this in the menu:

Project > Set programs' arguments...

This opens a window where you can insert your parameters.




回答2:


You need create a project before your code if you want you can click Project -> Set Program Arguments.




回答3:


The command line arguments in argv are arguments that are passed to your program on the command line when the program is executed. In order to take user input during program execution, you'll need to use more code, e.g. scanf or fgets.

If you're running your program from an IDE, there should be some way, e.g. project properties, to pass arguments to the program when you run it. For CodeBlocks, check the project menu: Project->Set Program Arguments.

If you can run your program in a terminal, you can pass arguments yourself, e.g.:

$ myProgram argument1

Then in your code, argv[1] will contain the string: "argument1".




回答4:


Code Blocks' Project - set programs' arguments (then type arguments in lower textbox of the pop-up dialog) should supply just what you type to your program when it starts. (CodeBlocks also can run on Ubuntu, as well as Windows, btw.) To make sure your code is processing arguments, you might Start - CMD.EXE under Win, navigate (e: cd \homework\projectname ) to your project directory, then cd bin\Debug. do a DIR and you'll see an EXE file. That is the part of your program which runs. Type its name, a space, then some arguments before you hit ENTER. See if your code is processing the args OK. (make sure main looks like: int main(int argc, char *argv[]) then in main() have a statement: cout << argv[1] << endl ; that should print the first word you type after the name of the EXE file.



来源:https://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!