write,Unix命令行程序和内建指令

更新时间:2022-10-19 02:28:45 阅读: 评论:0

基本简介

程序例

函数名: write

功 能: 写到一文件中

用 法: int write(int handle, void *buf, int nbyte);

程序例:

1234567891011121314151617181920212223242526272829303132333435#includeu003cstdlib.hu003e#includeu003cunistd.hu003e#includeu003cstdio.hu003e#includeu003cstring.hu003e#includeu003c#includeu003cerrno.hu003eint main(void){int handle;char string[40];int length,res;/*Create a file named TEST.$$$ in the current directory and writea string to it.If TEST.$$$ already exists,it will be over written.*/if((handle=open(TEST.$$$,O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1){printf(Erroropeningfile.n);exit(1);} length= if((res=write(handle,string,length))!=length){printf(Errorwritingtothefile.n);exit(1);} printf(Wrote%dbytestothefile.n,res);clo(handle);return 0;}
123456struct xfcb{char xfcb_flag;/*Contains0xfftoindicatexfcb*/char xfcb_resv[5];/*RervedforDOS*/char xfcb_attr;/*Searchattribute*/struct fcbxfcb_fcb;/*Thestandardfcb*/};

名称 : write

使用权限 : 所有使用者

使用方式 :

write ur [ttyname]

说明 : 传讯息给其他使用者

把计 :

ur : 预备传讯息的使用者帐号

ttyname : 如果使用者同时有两个以上的 tty 连线,可以自行选择合适的 tty 传讯息

例子.1 :

传讯息给 Rollaend,此时 Rollaend 只有一个连线 :

write Rollaend

接下来就是将讯息打上去,结束请按 ctrl+c

例子.2 :传讯息给 Rollaend,Rollaend 的连线有 pts/2,pts/3 :

write Rollaend pts/2

接下来就是将讯息打上去,结束请按 ctrl+c

注意 : 若对方设定 mesg n,则此时讯息将无法传给对方

Linux C

write(将数据写入已打开的文件内)

相关函数

open,read,fcntl,clo,lek,sync,fsync,fwrite

表头文件

#includeu003cunistd.hu003e

定义函数

ssize_t write (int fd,const void * buf,size_t count);

函数说明

write()会把指针buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动。

返回值

如果顺利write()会返回实际写入的字节数。当有错误发生时则返回-1,错误代码存入errno中。

错误代码

EINTR 此调用被信号所中断。

EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。

EBADF 参数fd非有效的文件描述词,或该文件已关闭。

本文发布于:2022-10-19 02:28:45,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/83/319404.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:write
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图