读写linux设备文件

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:29   2152   0
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define DIR "/dev/firstdriver"

int main(void)
{
 int fd;
 char arr[1000];
 char *str="hello china";
 fd = open(DIR, O_RDWR|O_NONBLOCK);
 if(-1!=fd)
 { 

  printf("The writen string is : %s\n",str);
  write(fd,str,strlen(str)+1); 
  read(fd,arr,strlen(str)+1);
  printf("%s\n", arr);
  
  close(fd);
  //write();
 }
 else
 {
  perror("read /dev/firstdriver");
 }
  
 return 0;
}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP