FS
open man 2 open
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main(){
int fd;
char *leds = "/dev/leds";
char *test1 = "/bin/test1";
char *test2 = "/bin/test2";
if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s failed!\n",leds);
}
printf("\n%s fd is %d\n",leds,fd);
if((fd = open(test1,O_RDWR,0777))<0){
printf("open %s failed!\n",test1);
}
printf("%s fd is %d\n",test1,fd);
if((fd = open(test2,O_RDWR|O_CREAT,0777))<0){
printf("open %s failed!\n",test2);
}
printf("%s fd is %d\n",test2,fd);
}
create
// 标准输入输出头文件
#include <stdio.h>
// 文件操作函数头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
int fd;
// 开发板中已经存在/dev/leds文件
char *leds = "/dev/leds";
// 开发板中不存在的文件/bin/test1
char *test1 = "/bin/test1";
// 开发板中不存在的文件/bin/test2
char *test2 = "/bin/test2";
// 需要新建的文件/bin/test3
char *test3 = "/bin/test3";
// 使用open函数打开文件
if ((fd = open(leds, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
{
printf("open %s failed\n", leds);
}
printf("%s fd is %d\n", leds, fd);
// 使用open函数打开不存在的文件,不添加O_CREAT标识符,会报错
if ((fd = open(test1, O_RDWR)) < 0)
{
printf("open %s failed\n", test1);
}
// 打开文件创建文件,添加标志位O_CREAT表示不存在这个文件则创建文件
if ((fd = open(test2, O_RDWR | O_CREAT, 0777)) < 0)
{
printf("open %s failed\n", test2);
}
printf("%s fd is %d\n", test2, fd);
fd = creat(test3, 0777);
if (fd = -1)
{
printf("%s fd is %d\n", test3, fd);
}
else
{
printf("create %s is succeed\n", test3);
}
}
write
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
main()
{
int fd;
char *testwrite = "/bin/testwrite";
ssize_t length_w;
char buffer_write[] = "Hello Write Function!";
if ((fd = open(testwrite, O_RDWR | O_CREAT, 0777)) < 0)
{
printf("open %s failed\n", testwrite);
}
length_w = write(fd, buffer_write, strlen(buffer_write));
if (length_w == -1)
{
perror("write");
}
else
{
printf("Write Function OK!\n");
}
close(fd);
}
read
// 标准输入输出头文件
#include <stdio.h>
// 文件操作函数头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_SIZE 1000
main()
{
int fd;
ssize_t length_w, length_r = MAX_SIZE, ret;
char *testwrite = "/bin/testwrite";
char buffer_write[] = "Hello Write Function!";
char buffer_read[MAX_SIZE];
if ((fd = open(testwrite, O_RDWR | O_CREAT, 0777)) < 0)
{
printf("open %s failed!\n", testwrite);
}
length_w = write(fd, buffer_write, strlen(buffer_write));
if (length_w == -1)
{
perror("write");
}
else
{
printf("Write Function OK!\n");
}
close(fd);
if ((fd = open(testwrite, O_RDWR | O_CREAT, 0777)) < 0)
{
printf("open %s failed!\n", testwrite);
}
if (ret = read(fd, buffer_read, length_r))
{
perror("read");
}
printf("Files Content is %s \n", buffer_read);
close(fd);
}
argvc
#include <stdio.h>
#include <string.h>
// argument count变元计数
// argument value变元值
int main(int argc, char *argv[])
{
int i, j;
i = atoi(argv[1]);
j = atoi(argv[2]);
printf("the Program name is %s\n", argv[0]);
printf("The command line has %d argument:\n", argc - 1);
printf("%d,%d\n", i, j);
return 0;
}
ioctl
led
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LED_NUM 2
#define LED_C 2
// cmd为0,则灭,为1,则亮;
// io为0则是靠近蜂鸣器的小灯,为1则靠近独立按键的小灯
int main(int argc, char *argv[])
{
int fd, led_num, led_c;
char *leds = "/dev/leds";
led_num = LED_NUM;
led_c = LED_C;
printf("argv1 is cmd;argv2 is io \n");
// 对传入的参数进行判断,超出范围直接退出
if (atoi(argv[1]) >= led_c)
{
printf("argv1 is 0 or 1)");
exit(1);
}
if (atoi(argv[2]) >= led_num)
{
printf("argv2 is 0 or 1)");
exit(1);
}
// 使用ioctl函数将参数传入内核
if ((fd = open(leds, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
printf("open %s failed\n", leds);
else
{
ioctl(fd, atoi(argv[1]), atoi(argv[2]));
printf("ioctl %s success\n", leds);
}
close(fd);
return (1);
}
ioctl
buzzer
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUZZER_C 2
int main(int argc, char *argv[])
{
char *buzzer_ctl = "/dev/buzzer_ctl";
int fd, ret, buzzer_c;
buzzer_c = BUZZER_C;
if (atoi(argv[1]) >= buzzer_c)
{
printf("argv[1] is 0 or 1\n");
exit(1);
}
if ((fd = open(buzzer_ctl, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
{
printf("open %s failed\n", buzzer_ctl);
exit(1);
}
ret = ioctl(fd, atoi(argv[1]), atoi(argv[2]));
close(fd);
return 0;
}
memset
adc
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <termios.h>
// #include <android/log.h>
// #include <sys/ioctl.h>
int main(void)
{
int fd;
char *adc = "/dev/adc";
char buffer[512];
int len = 0, r = 0;
memset(buffer, 0, sizeof(buffer));
printf("adc ready!\n");
if ((fd = open(adc, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
printf("open adc err!\n");
else
{
printf("open adc success!\n");
len = read(fd, buffer, 10);
if (len == 0)
printf("return null\n");
else
{
r = atoi(buffer);
r = (int)(r * 10000 / 4095); // Datas transition to Res
printf("res value is %d\n", r);
}
}
}
stat
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
struct stat groupstat;
int fd, ret;
if (argc < 2)
{
printf("\nPlease input file path\n");
return 1;
}
ret = stat(argv[1], &groupstat);
if (ret)
{
printf("Please make sure file path\n");
return 1;
}
printf("stat function test , %s of st_ino inode is %ld\n", argv[1], groupstat.st_ino);
fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("Please make sure file path\n");
return 1;
}
ret = fstat(fd, &groupstat);
if (ret)
{
printf("Please make sure file path\n");
return 1;
}
printf("fstat function test , %s of st_ino inode is %ld\n", argv[1], groupstat.st_ino);
ret = lstat(argv[1], &groupstat);
if (ret)
{
printf("Please make sure file path\n");
return 1;
}
printf("lstat function test , %s of st_ino inode is %ld\n", argv[1], groupstat.st_ino);
return 0;
}