linux c/c++ write() read () 函数的使用

write ( )

原型:#include<unistd.h>

            size_t write(int fd,const void *buf,size_t nbytes);

功能:把缓冲区buf 的前nbytes个字节写入与文件描述符fd关联的文件中

返回:返回实际写入的字节数,0 代表未写出数据 ;-1 调 write( )出错了 错误代码保存在 errno 里面

            0:标准输入

            1:标准输出

             -1:标准错误

 

[jl@localhost test]$ gcc writee.c -o writee                                                                                                                              
[jl@localhost test]$ ./writee                                                                                                                                            
here is some data
hahhahha
[jl@localhost test]$
 

把18改成17

[jl@localhost test]$ gcc writee.c -o writee
[jl@localhost test]$ ./writee
here is some datahahhahha

改成16
[jl@localhost test]$ gcc writee.c -o writee
[jl@localhost test]$ ./writee
here is some dathahhahha
[jl@localhost test]$

 

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
  write(1,"here is some data\n",18);//直接写到终端了 第18个字符是\n 17个是a 哦
  write(0,"hahhahha\n",9);
  printf(0,"%s");
  exit(0);//退出进程
}


 [jl@localhost test]$ ./writee
hheh
hheh
��  不知道哪的问题

[jl@localhost test]$

[jl@localhost test]$ ./writee
dfghkeishgkf
dfghkeishgkf
8㦿9?�|�
[jl@localhost test]$ ./writee
ertyuiopl
ertyuiopl
���9?�|�
[jl@localhost test]$ ./writee
wertyur
wertyur

[jl@localhost test]$ ./writee
erfhgjtnb
erfhgjtnb
d误�9?�|�
[jl@localhost test]$

[jl@localhost test]$ ./writee
rtyuhgfd          输入8个字符刚好没乱码
rtyuhgfd

[jl@localhost test]$


 

 

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
  char buff[10];
  int nread;
  
  nread=read(0,buff,10);//执行后会等待输入 输入完字符回车终端会显示刚输入的字符
  printf("%s\n",buff);
  return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值