嵌入式开发记录-day06 文件操作
一、文件IO操作函数《一》:
// 打开文件操作 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); // 创建文件操作 int creat(const char *pathname, mode_t mode); // 读文件操作 #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); // 写文件操作 #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);
二、文件操作实例
1、打开文件open()
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 6 main() 7 { 8 int fd; // 定义文件句柄 9 char *leds = "/dev/leds"; // 定义文件路径 10 char* test1 = "bin/test1"; 11 char* test2 = "/bin/test2"; 12 /* 13 O_RDONLY O_WRONLY O_RDWR // 可读可写方式打开文件 14 O_NOCTTY 若路径指向终端,则给设备不作为进程控制段 15 O_NDELAY 非阻塞操作文件 16 */ 17 fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY); // 打开成功返回0 18 if(fd < 0){ // 读写、指向终端、没有延迟 19 printf("leds open :%s failed\n",leds); 20 } 21 printf("open leds dev:%s sucess\n",leds); 22 23 if((fd = open(test1,O_RDWR,0777))<0) 24 { // 读写、指向终端、没有延迟 25 printf("test1 open :%s failed\n",test1); 26 } 27 printf("open dev leds:%s sucess\n",test1); 28 29 if(fd = open(test2,O_RDWR,0777)<0) 30 { // 读写、指向终端、没有延迟 31 printf("test2 open :%s failed\n",test2); 32 } 33 printf("open dev test2:%s sucess\n",test2); /// 一般打开文件需要关闭文件流,close(fd);此处没有关闭,需要注意 34 }
2、控制字符类设备ioctl(),控制LED,buzzer
控制LED,输入0 0或者1 0、0 1、1 1可以控制LED
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 #include <string.h> 6 #include <stdlib.h> 7 8 9 #define LED_NUM 2 // 个数 10 #define LED_C 2 // 状态on off 11 12 /* 控制字符设备 13 int ioctl(int fd,int request, int cmd) 14 fd文件句柄,request 可以表示io口,这些定义是由内核决定, 15 cmd进行怎么样操作 // 操作LED设备 16 */ 17 18 int main(int argc,char* argv[]) 19 { 20 int fd,led_num,led_c; 21 char* leds = "/dev/leds"; // LED灯设备结点 22 char* buzzer = "/dev/buzzer_ctl"; // 蜂鸣器设备结点 23 24 led_num = LED_NUM; 25 led_c = LED_C; 26 // cmd=0,led灭,cmd=1,on 27 printf("arg1 is cmd,arg2 is io\n"); 28 // 进行参数检查 29 if(atoi(argv[1]) >= led_c){ 30 printf("argv[1] is 0 or 1\n"); 31 exit(1); 32 } 33 if(atoi(argv[2]) >= led_num){ 34 printf("argv[2] is 0 or 1\n"); 35 exit(1); 36 } 37 // 打开字符设备 38 if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){ 39 printf("file open failed\n"); 40 exit(1); 41 } 42 else{ 43 ioctl(fd,atoi(argv[1]),atoi(argv[2]));// 对LED进行操作 44 printf("func ioctl %s sucess\n",leds); 45 } 46 // 关闭打开的数据流文件 47 close(fd); 48 49 printf("hello world\n"); 50 return 0; 51 }
控制Buzzer
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 #include <string.h> 6 #include <stdlib.h> 7 8 #define BUZZER_C 2 9 10 int main(int argc,char* argv[]) 11 { 12 char* buzzer = "/dev/buzzer_ctl"; // 定义字符设备结点 13 int fd,ret,buzzer_c; 14 buzzer_c = BUZZER_C; 15 16 if(atoi(argv[1]) >= buzzer_c){ 17 printf("argv[1] is 0 or 1\n"); 18 exit(1); 19 } 20 // 打开设备Buzzer 21 if((fd = open(buzzer,O_RDWR|O_NOCTTY|O_NDELAY))< 0 ){ 22 printf("open %s failed\n",buzzer); 23 exit(1); 24 } 25 ret = ioctl(fd,atoi(argv[1])); // 控制蜂鸣器 26 close(fd); 27 return 0; 28 }
3、读取字符设备ADC
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 #include <string.h> 6 #include <stdlib.h> 7 #include <unistd.h> 8 9 void main(void) 10 { 11 int fd; 12 char* adc = "/dev/adc"; 13 char buff[512]; 14 int len = 0; 15 float tmp; 16 17 memset(buff,0,sizeof(buff)); 18 printf("adc ready!\n"); 19 // 内核会默认读取10次,也会打印出所需要的ADC 20 if((fd = open(adc,O_RDWR|O_NOCTTY|O_NDELAY)) < 0){ 21 printf("open %s err\n",adc); 22 exit(1); 23 } 24 else{ 25 printf("open adc sucess\n"); 26 len = read(fd,buff,10); 27 if(len == 0){ 28 printf("return null\n"); 29 exit(1); 30 }else{ 31 tmp = (float)atoi(buff); 32 tmp = (tmp * 10000/4095); 33 printf("adc valuce is %f\n",tmp); 34 } 35 } 36 }

浙公网安备 33010602011771号