read函数头文件 window_在C语言中要用到write和read函数要用到什么头文件

展开全部

1、要用到unistd.h头文e5a48de588b662616964757a686964616f31333337616632件。

2、 Write函数

用法:

write函数所在的头文件为

write有两种用法。一种是:

ssize_twrite(int handle, void *buf, int nbyte);

handle 是文件描述符;

buf是指定的缓冲区,即指针,指向一段内存单元;

nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)

write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.

另一种是:write(const char* str,int n)

str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。

write("string",strlen("string");表示输出字符串常量

3、程序示例:#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.\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; }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值