我想用 C 或 Objective-C 编写一个简单的交互式命令行程序。提示用户输入,然后对该输入进行操作,然后提示用户进行更多输入。我在谷歌上搜索了“交互式命令行应用程序”以及该主题的几个变体,但没有提出我正在寻找的任何简单示例。
这看起来像是一个绝对初级、基础的编程示例,就像“hello world”之后的一步。谁能向我指出此类程序的示例,或者告诉我应该搜索什么?
I'd like to write a simple interactive command line program in C or Objective-C. Something that prompts the user for input and then acts on that input, then prompts the user for more input. I've googled "interactive command line application" and several variations on that theme but am not coming up with any simple examples of what I'm looking for.
This seems like an absolutely elementary, fundamental programming example, like a step after "hello world". Can anyone point me to an example of such a program, or tell me what I should be searching for?
发布评论
评论(3)
Xcode 4 - 基础命令行工具
main.m
文件注意:如果未出现命令行界面,请点击第二个按钮:
现在您可以与命令行工具进行交互。
Xcode 4 - Foundation Command Line Tool
main.m
fileNote: In case the command line interface does not appear, hit the second button:
Now you can interact with the command line tool.
有很多方法可以将输入输入到程序中。最基本的之一是 scanf。
There are many ways you can get input into a program. One of the most basic is scanf.
你现在得到了一个已取消的项目,其中一些东西已经制作完成。您在第一个“NSLog”语句所在的位置键入代码。只需删除该语句,然后输入您自己的语句即可。
注意:NSLog 是一种 Objective-C 形式,用于在控制台中打印内容,并且对于每个 NSLog 调用,它都会出现在新行上。
printf 是做同样事情的 c 方法,但是为了打印到新行,您必须使用 \n - 作为约定, printf 调用总是以 \n 结尾是正常的,
请自己尝试一下,接受这个进入命令行项目(在已经有 NSLog 语句的地方) - 将其替换为 2 个 NSLog 调用和 2 个 printf 函数。
you now get a stubbed-out project, with some stuff already made. You type your code where is has the first "NSLog" statement. Simply remove that statement, and type in your own.
Notice: NSLog is an objective-C form of getting stuff printed in the console, and for each NSLog call it will appear on a new line.
Printf is the c way of doing the same thing, but in order to print to a new line you must use \n - as a convention, it is normal that printf calls always ends with a \n
Try it out for yourself, take this into a command line project (at the place where it already has an NSLog statement) - replace it with 2 NSLog calls and 2 printf functions.