demo.c

来自「一个Linux系统字符驱动的源代码」· C语言 代码 · 共 47 行

C
47
字号
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <fcntl.h>int main(){	int fd,i,nwrite,nread;	char *buf="HELLO WORLD!";	char read_buf[1024]={0}; // user space buffer	fd=open("/dev/szx",O_RDWR);	if(fd<=0)	{		perror("ERROR in opening this file, please CHECK the device!");		exit(1);	}	else		printf("File open SUCCESSFUL!\n");            nwrite=write(fd,buf,strlen(buf)); // write data to myfs		if(nwrite<0)	{		perror("ERROR in writing this file!");		exit(1);	}    printf("PRESS ANY KEY TO CONTINUE...");	getchar();	nread=read(fd,read_buf,1024);  //read data from myfs	if(nread<0)	{		perror("ERROR in reading this file!");		exit(1);	}	else		printf("The written string is:  %s\n",read_buf);		close(fd);	exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?