新闻资讯
看你所看,想你所想

write(Unix命令行程式和内建指令)

write(Unix命令行程式和内建指令)

write(Unix命令行程式和内建指令)

write是一个Unix命令行程式和内建指令,功能是写到一档案中,用法是int write(int handle, void *buf, int nbyte)。

基本介绍

  • 函式名:write()
  • 功能:写到档案中
  • 函式原型:int write()
  • 所在头档案:unistd.h

程式例

函式名: write
功 能: 写到一档案中
用 法: int write(int handle, void *buf, int nbyte);
程式例:
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>#include<fcntl.h>#include<errno.h>int 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);}strcpy(string,"Hello,world!\n");length=strlen(string);if((res=write(handle,string,length))!=length){printf("Errorwritingtothefile.\n");exit(1);}printf("Wrote%dbytestothefile.\n",res);close(handle);return 0;}
struct xfcb{char xfcb_flag;/*Contains0xfftoindicatexfcb*/char xfcb_resv[5];/*ReservedforDOS*/char xfcb_attr;/*Searchattribute*/struct fcbxfcb_fcb;/*Thestandardfcb*/};
名称 : write
使用许可权 : 所有使用者
使用方式 :
write user [ttyname]
说明 : 传讯息给其他使用者
把计 :
user : 预备传讯息的使用者帐号
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,close,lseek,sync,fsync,fwrite

表头档案

#include<unistd.h>

定义函式

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非有效的档案描述词,或该档案已关闭。

转载请注明出处海之美文 » write(Unix命令行程式和内建指令)

相关推荐

    声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:ailianmeng11@163.com