Linux文件操作

一,系统编程概述

        Linux系统编程是指使用系统调用或者C语言本身所携带的库函数来编写具有某一特定功能的程序!

Shell命令是OS提供给用户使用的接口,而系统调用OS提供给程序员使用的接口。如,作为系统调用提供的open函数用于打开一个文件。实际上,C语言的库函数也是通过系统调用的来实现的,它封装了系统调用,在此基础上为了让程序员方便而增加的一些功能。

         Linux为上层应用程序的开发提供了丰富的系统调用,应用程序只需要包含相应的头文件就可以使用这些函数。系统调用都是以库函数的方式提供。编译程序时,gcc会自动链接一些常用库,比如libc.so.6。gcc不会自动链接的库,则需要在编译程序时指定使用的库。

二,文件的访问权限控制

  1.         drwxrwx-w-         r=4    w=2    x=1

在C中,chmod/fchmod函数对文件的访问的权限进行修改

/*
2019-4-23
my_chmod.c
*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char **argv)
{
    int mode;       //permission
    int mode_u;
    int mode_g;
    int mode_o;
    char *path;
    //check the arg
    if(argc<3){
        printf("%s <mode num> <target file>\n",argv[0]);
        exit(0);
    }
    //from the terminal get the arg
    mode=atoi(argv[1]);
    if(mode>777||mode<0){
        printf("mode num error!");
        exit(0);
    }
    mode_u=mode/100;
    mode_g=(mode-(mode_u*100))/10;
    mode_o=mode-(mode_u*100)-(mode_g*10);
    mode=(mode_u*8*8)+(mode_g*8)+mode_o;    //oct change
    path=argv[2];
    if(chmod(path,mode)==-1){
        perror("chmod error");
        exit(1);
    }
    return 0;
}

三、文件创建,打开与关闭

1,open函数

man 2 open

2,create函数

man creat

//my_create.c

#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=open("./my_chmod2.c",O_CREAT|O_EXCL,S_IRUSR|S_IWUSR)==-1)){
        perror("open");
        _exit(0);
    }else{
        printf("Open success!");
    }
}

四,文件的读写

1,read函数

 参数含义:

int fd:      the file descriptor(文件描述符)

void *buff:the buffer(缓冲区)

size_t count:buffer size(字节数大小)

2,write函数

3 ,文件读写指针的移动

lseek函数

每一个已打开的文件都有一个读写位置,当打开文件时通常读写位置指向文件开头,诺是以添加的方式打开文件(调用open函数时使用了O_APPEND),则读写位置会指向文件尾。通过lseek来控制该文件的读写位置。

参数解析:

off_t offset:为根据whence来移动读写位置的位移数。

whence:文件指针位置

//my_lseek.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
int main(){
    char buf[]={"Hello China!\n"};
    char outbuf[10];
    int len;
    int fd=open("./Test",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
    if(write(fd,buf,strlen(buf))==-1){
        perror("write");
    }
    len=lseek(fd,6,SEEK_SET);
    printf("%d\n",len);
    read(fd,outbuf,11);
    printf("%s",outbuf);
}

运行结果:

[alex@China c]$ ./my_lseek 
6
China!

 4,显示文件信息

//my_chmod_list.c
#include<stdio.h>
#include<time.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
int main(int argc,char *argv[]){
    struct stat buf;
    stat(argv[1],&buf);
    
    printf("device is:%d\n",buf.st_dev);
    printf("inode is:%d\n",buf.st_ino);
    printf("mode is:%o\n",buf.st_mode);
    printf("number of hard links is:%d\n",buf.st_nlink);
    printf("user ID of owner is:%d\n",buf.st_uid);
    printf("group ID of owner is:%d\n",buf.st_gid);
    printf("device type is:%d\n",buf.st_rdev);

    printf("total size,in bytes is:%d\n",buf.st_size);
    printf("blocksize for filesystem I/O  is:%d\n",buf.st_blksize);
    printf("number of blocks allocated is:%d\n",buf.st_blocks);
    
    printf("time of last access is:%s\n",ctime(&buf.st_atime));
    printf("time of last modification is is:%s\n",ctime(&buf.st_mtime));
    printf("time of last change  is:%s\n",ctime(&buf.st_ctime));
    return 0;
}

运行结果:

[alex@China c]$ ./my_chmod_list my_chmod
device is:64768
inode is:103485821
mode is:100775
number of hard links is:1
user ID of owner is:1000
group ID of owner is:1000
device type is:0
total size,in bytes is:8648
blocksize for filesystem I/O  is:4096
number of blocks allocated is:24
time of last access is:Tue Apr 23 22:55:03 2019

time of last modification is is:Tue Apr 23 22:54:55 2019

time of last change  is:Tue Apr 23 22:54:55 2019
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值