write是一個Unix命令行程式和內建指令,功能是寫到一檔案中,用法是int write(int handle, void *buf, int nbyte)。
基本介紹
- 函式名:write()
- 功能:寫到檔案中
- 函式原型:int write()
- 所在頭檔案:unistd.h
程式例
#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*/};