【问题标题】:System Calls Function for prompting and getting user input用于提示和获取用户输入的系统调用函数
【发布时间】:2013-09-23 07:32:49
【问题描述】:
好的,我正在为我的班级编写一个 C 程序,但我只能使用系统调用。基本上,我们的程序正在制作我们的 on cp 命令,我们从命令行获取两个文件作为输入,然后复制第一个文件并将其放入第二个文件中。它相对简单,我的大部分代码都正确或几乎正确,可能还有一些小修复。但是,程序的一部分是如果目标文件已经存在,我们需要提示用户询问是否应该覆盖它,所以我需要知道如何使用系统调用函数获取用户输入,也就是我可以不要使用 scanf、fgets、gets 等。我可以从标准库中使用的唯一函数基本上是 printf。所以我需要知道系统调用函数是什么来获得用户提示。这部分代码应该像 cp -i 一样工作,如果这对任何人都有帮助的话。提前谢谢你。
【问题讨论】:
标签:
c
user-input
system-calls
【解决方案1】:
您可以使用系统调用read。要从标准输入读取,fd(文件描述符)为 0。
$ man read
阅读(2)
Linux程序员手册(二)
名字
read - read from a file descriptor
概要
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
描述
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.