欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1-3、讯为系统编程write

程序员文章站 2024-03-08 10:42:28
...

关闭文件close函数

int close(int fd);

写文件write函数

ssize_t write(int fd,const void *buf,size_t count);
  • 参数fd表示:使用open函数打开文件之后返回的句柄
  • 参数*buf表示:写入的数据
  • 参数count表示:最多写入字节数
  • 返回值:出错-1,气他数值表示实际写入的字节数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

main()
{	
	int fd;
	char *testwrite = "/bin/testwrite";
	char buffer_write[] = "Hello Write Function!";
	int length_w;
	
	if((fd = open(testwrite,O_RDWR|O_CREAT,0777))<0)
	{
		printf("open %s failed!\n",testwrite);
	}
	length_w = write(fd,buffer_write,strlen(buffer_write));
	
	if(length_w == -1)
	{
		perror("write");
	}
	else
	{
		printf("write function OK!\n");
	}
	close(fd);
}

 

相关标签: 讯为iTop4412