如何从命令行调用函数 [英] How to call functions from the command line

查看:112
本文介绍了如何从命令行调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个c ++程序,它接收二进制文件并对其执行各种操作。我不会深入了解操作的细节,目前我通过调用不同的函数来指定操作的顺序,就像我编写main函数一样。我的问题是 - 用户是否可以通过命令行从该应用程序调用不同的函数。

例如。


我说我有3个功能,例如:Open,F1,F2,F3。


现在我想在程序运行时调用这些函数,我明白我将无法做任何其他事情程序运行时的命令行。它是一个应用程序,所以我想首先运行程序,然后在程序运行时调用不同的函数,如:


a.out打开(文件名)

a.out CallF1(x,y,z)

(在F1产生结果后,例如Woohoohoo然后

a.out CallF2(a,b,c)







和最后


a .out退出。


意味着从参数命令行调用不同的函数。

任何帮助实现上述功能的主函数结构非常感谢。

解决方案

我假设当你提到命令行时,你正在谈论在程序执行时在键盘输入数据(而不是你的命令行)启动程序)??最简单的
您可以从键盘读取一个数字或字符,并根据其值选择您想要的操作

展开 | 选择 | < span class =codeLinkonclick =WordWrap(this);> Wrap | 行号


我建​​议使用命令行参数。查找argc和argv []。一旦你从命令行得到了参数,在名称的情况下用每个函数实现一个switch语句应该是微不足道的。



在最简单的级别你可以从键盘上读取一个数字或字符,并根据它的值选择你想要做的操作

展开 | 选择 | Wrap | 行号

Hi all,

I have a c++ programme that intakes a binary file and performs various operations on it. I am not gonna get in to the details of the operations, currently i specify the order in which the operations take place by calling different functions the way i write the main function. My question is -- Is there a way that a user can call different functions from this application from the command line.
For example.

say i have 3 functions e.g : Open,F1 , F2, F3.

Now i would like to call the functions while the programme is running and i understand that i wont be able to do anything else at the command line while the prograame is running. Its an application so i wanna run the programme first and then call different functions while the programme is running such as:

a.out Open (filename)
a.out CallF1 (x,y,z)
(after F1 produces its result e.g "Woohoohoo" then
a.out CallF2 (a,b,c)
.
.
.
and Finally

a.out Quit.

meaning call different functions from the command line with the parameters.
Any help with the structure of the main function to acheive the above will be highly appreciated.

解决方案

I assume when you mention command line you are talking about entering data at the keyboard while the program is executing (rather than the comand line when you start the program)??
at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do

Expand|Select|Wrap|Line Numbers


I would suggest using command-line arguments. Look up argc and argv[]. Once you''ve got the arguments from command-line, it should be trivial to implement a switch statement with each function in the case of its name.


at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do

Expand|Select|Wrap|Line Numbers


这篇关于如何从命令行调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆