第25节:creat创建操作
1、编写运行编译creat函数例程:
root@ubuntu:/home/linuxsystemcode/create# cat creat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main(){
int fd;
char *leds = "/dev/leds";
char *test1 = "/bin/test1";
char *test2 = "/bin/test2";
printf("open %s failed!\n",leds);
}
printf("%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);
fd = creat(test3,0777);
if(fd == -1){
printf("%s fd failed,fd is %d\n",test3);
}
else{
printf("creat %s fd is succesed! fd is %d\n",test3,fd);
}
}
2、进行文件编译
root@ubuntu:/home/linuxsystemcode/create# arm-none-linux-gnueabi-gcc -o creat creat.c
root@ubuntu:/home/linuxsystemcode/create# ls
creat creat.c
3、将文件拷贝到TF卡上,并传入开发板上运行结果
[root@iTOP-4412]# ./creat
[ 1388.047982] LEDS_CTL DEBUG:Device Opened Success!
[ 1388.051580] LEDS_CTL DEBUG:Device Closed Success!
/dev/leds fd is 3
open /bin/test1 failed!
/bin/test1 fd is -1
/bin/test2 fd is 4
creat /bin/test3 fd is succesed! fd is 5
1、编写运行编译creat函数例程:
root@ubuntu:/home/linuxsystemcode/create# cat creat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main(){
int fd;
char *leds = "/dev/leds";
char *test1 = "/bin/test1";
char *test2 = "/bin/test2";
char *test3 = "/bin/test3";
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);
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);
fd = creat(test3,0777);
if(fd == -1){
printf("%s fd failed,fd is %d\n",test3);
}
else{
printf("creat %s fd is succesed! fd is %d\n",test3,fd);
}
}
2、进行文件编译
root@ubuntu:/home/linuxsystemcode/create# arm-none-linux-gnueabi-gcc -o creat creat.c
root@ubuntu:/home/linuxsystemcode/create# ls
creat creat.c
3、将文件拷贝到TF卡上,并传入开发板上运行结果
[root@iTOP-4412]# ./creat
[ 1388.047982] LEDS_CTL DEBUG:Device Opened Success!
[ 1388.051580] LEDS_CTL DEBUG:Device Closed Success!
/dev/leds fd is 3
open /bin/test1 failed!
/bin/test1 fd is -1
/bin/test2 fd is 4
creat /bin/test3 fd is succesed! fd is 5