write
函数名: write
功 能: 写到一文件中
用 法: int write(int handel, void *buf, int nbyte);
程序例:
#include
#include
#include
#include
#include
#include
int main(void)
{
int handle;
char string[40];
int length, res;
/*
Create a file named "TEST.$$$" in the current directory and write
a string to it. If "TEST.$$$" already exists, it will be overwritten.
*/
if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE)) == -1)
{
printf("Error opening file.
");
exit(1);
}
strcpy(string, "Hello, world!
");
length = strlen(string);
if ((res = write(handle, string, length)) != length)
{
printf("Error writing to the file.
");
exit(1);
}
printf("Wrote %d bytes to the file.
", res);
close(handle);
return 0;
}
struct xfcb {
char xfcb_flag; /* Contains 0xff to indicate xfcb */
char xfcb_resv[5]; /* Reserved for DOS */
char xfcb_attr; /* Search attribute */
struct fcb xfcb_fcb; /* The standard fcb */
}; 名称 : 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 Cwrite(将数据写入已打开的文件内)
相关函数
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),若无数据可读取则返回此值。
EADF 参数fd非有效的文件描述词,或该文件已关闭。