Linux C 程序 文件操作(Linux系统编程)(14)

文件操作(Linux系统编程)

创建一个目录时,系统会自动创建两个目录.和..


C语言实现权限控制函数

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #include<sys/types.h>

 4 #include<sys/stat.h>

 5 

 6 int main(int argc , char **argv){

 7         int mode;

 8         int mode_u;

 9         int mode_g;

10         int mode_o;

11         char *path;

12 

13         if(argc < 3){

14                 printf("%s<mode num> <target file>\n",argv[0]);

15                 exit(0);

16         }

17                 //字符串转换成整型

18         mode = atoi(argv[1]);

19         if(mode > 777 || mode < 0){

20                 printf("mode num error ! \n");

21                 exit(0);

22         }

23 

24         mode_u = mode / 100;

25         mode_g = (mode - (mode_u*100)) / 10;

26         mode_o = mode - (mode_u*100) - (mode_g*10);

27         mode = (mode_u * 8 * 8) + (mode_g * 8) + mode_o;

28         path = argv[2];

29                 //改变权限函数

30         if(chmod(path , mode) == -1){

31                 perror("chmod error");

32                 exit(1);

33         }

34 

35         return 0;

36 }

37     

38 新建一个文件test.c

39 命令:./程序 444  test.c

 2.文件的输入输出函数

 1 #include<stdio.h>

 2 #include<sys/types.h>

 3 #include<sys/stat.h>

 4 #include<fcntl.h>

 5 #include<unistd.h>

 6 #include<errno.h>

 7 

 8 int main(){

 9         int fd;

10                 //文件不存在创建文件

11         if ((fd = open("example.c",O_CREAT|O_EXCL,S_IRUSR |S_IWUSR)) == -1){

12                //       if((fd = creat("example.c",S_IRWXU)) == -1){

13               //        perror("open");

14                         printf("open:%s with errno :%d \n", strerror(errno),errno);

15                         exit(1);

16                 }else{

17                         printf("create file success\n");

18                 }

19                         //}

20 

21         close(fd);

22         return 0;

23 

24 }

25 

26 

27 my_creat.c: In function ‘main’:

28 my_creat.c:13: warning: incompatible implicit declaration of built-in function ‘exit’

 

3.文件的读写
文件读写和文件读写指针的移动操作
    

 1 #include<stdio.h>

 2 #include<sys/types.h>

 3 #include<sys/stat.h>

 4 #include<fcntl.h>

 5 #include<unistd.h>

 6 #include<errno.h>

 7 

 8 /**error handle function **/

 9 void my_err(const char *err_string , int line){

10         fprintf(stderr,"line:%d",line);

11         perror(err_string);

12         exit(1);

13 }

14 /**read data function**/

15 int my_read(int fd){

16         int len;

17         int ret;

18         int i;

19         char read_buf[64];

20         /**get file length and save file-read-pointer to the head of file **/

21         if(lseek(fd , 0 , SEEK_END) == -1){

22                 my_err("lseek",_LINE_);

23         }

24         if((len = lseek(fd,0,SEEK_CUR)) == -1){

25                 my_err("lseek",_LINE_);

26         }

27         if((lseek(fd,0,SEEK_SET)) == -1){

28                 my_err("lseek",_LINE_);

29         }

30 

31         printf("len :%d\n",len);

32         /**read data**/

33         if((ret = read(fd , read_buf , len)) < 0){

34                 my_err("read",_LINE_);

35         }

36         /**print data**/

37         for(i = 0 ; i < len ; i++){

38                 printf("%c",read_buf[i]);

39         }

40         printf("\n");

41 

42         return ret;

43 }

44 int main(){

45         int fd ;

46         char write_buf[32] = "hello world!";

47         /**create file in current dictionary**/

48         if((fd = creat("example1.c",S_IRWXU)) == -1){

49                 if((fd = open("example1.c",O_RDWR|O_CREAT|O_TRUNC,S_IRWXU)) == -1){

50                         my_err("open",_LINE_);                                                                                                                                                                                                                      1,17          Top

51                }else{

52                         printf("create file success!");

53                 }

54                 /*wirte data*/

55                 if(wirte(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){

56                         my_err("write",_LINE_);

57                 }

58                 my_read(fd);

59 

60                 /**display file's space**/

61                 printf("--------------------\n");

62                 if(lseek(fd,10,SEEK_END) == -1){

63                         my_err("lseek",_LINE_);

64                 }

65 

66                 if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){

67                         my_err("write",_LINE_);

68                 }

69                 my_read(fd);

70 

71                 close(fd);

72 

73                 return 0;

74         }

75 }

 


   

你可能感兴趣的:(linux)