write函数

中文名 write函数
函数名 write
目录导航

  write

  写到一文件中

  int write(int handel, void *buf, int nbyte);
  handel 是 文件描述符;
  buf是你指定的缓冲区,即指针,指向一段内存单元;
  nbyte是你要写入文件指定的字节数;
   返回值:写入文档的字节数(成功);-1(出错)

  #include <stdio.h>
  #include <stdlib.h>
  #include <fcntl.h>
  #include <sys\stat.h>
  #include <io.h>
  #include <string.h>
  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.\n");
  exit(1);
  }
  strcpy(string, "Hello, world!\n");
  length = strlen(string);
  if ((res = write(handle, string, length)) != length)
  {
  printf("Error writing to the file.\n");
  exit(1);
  }
  printf("Wrote %d bytes to the file.\n", res);
  close(handle); return 0; }

相关百科
返回顶部
产品求购 求购