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

时间:2023-03-09 16:19:23
Linux C 程序 文件操作(Linux系统编程)(14)

文件操作(Linux系统编程)

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

C语言实现权限控制函数

 #include<stdio.h>
 #include<stdlib.h>
 #include<sys/types.h>
 #include<sys/stat.h>

 int main(int argc , char **argv){
         int mode;
         int mode_u;
         int mode_g;
         int mode_o;
         char *path;

         ){
                 printf(]);
                 exit();
         }
                 //字符串转换成整型
         mode = atoi(argv[]);
          || mode < ){
                 printf("mode num error ! \n");
                 exit();
         }

         mode_u = mode / ;
         mode_g = (mode - (mode_u*)) / ;
         mode_o = mode - (mode_u*) - (mode_g*);
         mode = (mode_u *  * ) + (mode_g * ) + mode_o;
         path = argv[];
                 //改变权限函数
         ){
                 perror("chmod error");
                 exit();
         }

         ;
 }

 新建一个文件test.c
 命令:./程序   test.c

2.文件的输入输出函数

 #include<stdio.h>
 #include<sys/types.h>
 #include<sys/stat.h>
 #include<fcntl.h>
 #include<unistd.h>
 #include<errno.h>

 int main(){
         int fd;
                 //文件不存在创建文件
         ){
                //       if((fd = creat("example.c",S_IRWXU)) == -1){
               //        perror("open");
                         printf("open:%s with errno :%d \n", strerror(errno),errno);
                         exit();
                 }else{
                         printf("create file success\n");
                 }
                         //}

         close(fd);
         ;

 }

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

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

 #include<stdio.h>
 #include<sys/types.h>
 #include<sys/stat.h>
 #include<fcntl.h>
 #include<unistd.h>
 #include<errno.h>

 /**error handle function **/
 void my_err(const char *err_string , int line){
         fprintf(stderr,"line:%d",line);
         perror(err_string);
         exit();
 }
 /**read data function**/
 int my_read(int fd){
         int len;
         int ret;
         int i;
         ];
         /**get file length and save file-read-pointer to the head of file **/
          , SEEK_END) == -){
                 my_err("lseek",_LINE_);
         }
         ,SEEK_CUR)) == -){
                 my_err("lseek",_LINE_);
         }
         ,SEEK_SET)) == -){
                 my_err("lseek",_LINE_);
         }

         printf("len :%d\n",len);
         /**read data**/
         ){
                 my_err("read",_LINE_);
         }
         /**print data**/
          ; i < len ; i++){
                 printf("%c",read_buf[i]);
         }
         printf("\n");

         return ret;
 }
 int main(){
         int fd ;
         ] = "hello world!";
         /**create file in current dictionary**/
         ){
                 ){
                         my_err(,          Top
                }else{
                         printf("create file success!");
                 }
                 /*wirte data*/
                 if(wirte(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
                         my_err("write",_LINE_);
                 }
                 my_read(fd);

                 /**display file's space**/
                 printf("--------------------\n");
                 ,SEEK_END) == -){
                         my_err("lseek",_LINE_);
                 }

                 if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
                         my_err("write",_LINE_);
                 }
                 my_read(fd);

                 close(fd);

                 ;
         }
 }