查看: 2224| 回复: 14
跳转到指定楼层
上一主题 下一主题
收起左侧

[学C/C++] 关于C语言程序的一个问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(int argc, char *argv[])
  4. {
  5.     int fdTiger;

  6.     char buf[11];
  7.     int ret;

  8.     fdTiger = 0;

  9.     while(1)
  10.     {
  11.        memset((void *) buf, 0, 11);

  12.        ret = read(fdTiger, (void *) buf, 5);

  13.        printf("ret = %d\n", ret);

  14.        if(ret != -1)
  15.        {      /* buf[10] = '\0'; */
  16.               printf(" buf = %s\n", buf);
  17.        }
  18.     }

  19.      return 0;

  20. }
复制代码




这是一个C语言程序。就是说,比如我键盘输入  ”ADSFASDGSDFGAKASBXIuqwdb  aisaskcjni liabsd kas d“

就返回

ret = 5
buf = ADSFA
ret = 5
buf = SDGSD
ret = 5
buf = FGAKA
ret = 5
buf = SBXIu
ret = 5
buf = qwdb
ret = 5
buf =  aisa
ret = 5
buf = skcjn
ret = 5
buf = i lia
ret = 5
buf = bsd k
ret = 5
buf = as d



也就是说把键盘舒服的内容,按照5个字母切出来,然后打印出来。


我的问题是,关于变量fdTiger, 还有ret = read(fdTiger, (void *) buf, 5);  --> 这一行是怎么知道要把fdTiger当作如键盘输入的读取指针?  加入我初始化多一个变量,程序怎么辨认谁是指针呢?


留言的我都回加米。也求一粒米

评分

参与人数 2大米 +3 收起 理由
mgal059 + 1 很有用的信息!
14417335 + 2

查看全部评分


上一篇:求推荐的前端React的课程
下一篇:刷题语言和现实工作语言
推荐
striges1111 2020-12-6 11:07:39 | 只看该作者
全局:
本帖最后由 striges1111 于 2020-12-6 11:18 编辑

c语言不需要先声明函数才能调用,只要编译器能猜出来函数的原型是什么,就可以用。IOCCC的基本技巧之一

open()是一个system call(系统调用,注意系统调用里面的open是没有参数的),open会返回一个file descriptor,注意不要和fopen函数搞混淆了,fopen返回一个FILE类型的指针。一个file descriptor就是一个int,可以在/proc/(pid)/fd里面看到对应的symlink。其中0,1,2这三个必然存在,会指向某个/dev/pts/某个东西,具体的,那您得再去读一下kernel的内容了。


补充一下,man里面的open(2)是syscall, open(3)是fnctl.h里面定义的open函数,这两个也不要混淆了,虽然本质上open(3)就是调用了open这个syscall,但是调用syscall需要用汇编语言在寄存器上设置好参数然后调用80中断。因为太罗嗦所以有了glibc

评分

参与人数 1大米 +1 收起 理由
techDiscussion + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

全局:
http://codewiki.wikidot.com/c:system-calls:read
这里写得很清楚了:

ssize_t read(int fildes, void *buf, size_t nbytes);
int fildes: The file descriptor of where to read the input. You can either use a file descriptor obtained from the open system call, or you can use 0, 1, or 2, to refer to standard input, standard output, or standard error, respectively.

你的 fdTiger = 0,所以指向standard input,在C里面就是对应keyboard input.

不过你漏了#include <unistd.h>

评分

参与人数 1大米 +1 收起 理由
techDiscussion + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

🔗
sshou14 2020-12-6 09:16:30 | 只看该作者
全局:
其实不太懂你的意思,read 的第一个argument是一个file descriptor,fdTiger = 0 所以pass 了 0,所以就是 stdin
详情可以man read
回复

使用道具 举报

全局:
不知道你的read function是哪一个库的,如果是以下链接的,感觉这就是根据里面的参数数量来决定的。 https://pubs.opengroup.org/onlin ... functions/read.html

评分

参与人数 1大米 +1 收起 理由
techDiscussion + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

🔗
 楼主| techDiscussion 2020-12-6 10:50:27 | 只看该作者
全局:
晨风晚霞 发表于 2020-12-6 09:34
http://codewiki.wikidot.com/c:system-calls:read
这里写得很清楚了:

同学你好,我没有用#include <unistd.h>  一样可以运行啊?  

我的问题是,

---->  The file descriptor of where to read the input.
----> You can either use a file descriptor obtained from the open system call, or you can use 0, 1, or 2, to refer to standard input, standard output, or standard error, respectively.


所以我这里的fd_Tiger就是 “a file descriptor obtained from the open system call”?  


怎么理解: “a file descriptor obtained from the open system call“ ?
回复

使用道具 举报

🔗
晨风晚霞 2020-12-6 11:24:07 | 只看该作者
全局:
本帖最后由 晨风晚霞 于 2020-12-6 11:26 编辑
techDiscussion 发表于 2020-12-6 10:50
同学你好,我没有用#include   一样可以运行啊?  

我的问题是,

你这里的fd_tiger 不是 file descriptor, 而只是单纯传递一个数字0
因为你并没有用fd_tier存fopen之类的返回值。你没有include也能编译是因为你在linux下编译环境可能已经自带了一系列essential的header/library
你如果放到windows下VS或者mac下试试编译就会报错了
回复

使用道具 举报

全局:
帮顶,请加米hhh
回复

使用道具 举报

🔗
feitao 2020-12-6 12:52:23 | 只看该作者
全局:
https://man7.org/linux/man-pages/man2/read.2.html

从哪里看到的程序,建议换本书
回复

使用道具 举报

🔗
sshou14 2020-12-6 13:06:33 | 只看该作者
全局:
怎么理解: “a file descriptor obtained from the open system call“ ?
> 请看 `man 2 open`的介绍 "Given a pathname for a file, open() returns a file descriptor..."

The file descriptors for stdin, stdout, and stderr are 0, 1, and 2. 你pass 的是0,所以stdin

评分

参与人数 1大米 +1 收起 理由
techDiscussion + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表