linux文件操作知识点

ln 命令在不同目录中创建指向同一个文件的连接

/dev/console 系统控制台

/dev/tty 进程的控制终端(键盘和显示器,或键盘和窗口)的别名

/dev/null 空设备, cp命令可以把它用作拷贝空文件的源文件。而touch作用是来改变文件的修改时间,文件不存在才创建,但并不会清空文件。

 

访问设备驱动程序的底层函数(open, read, write, close, ioctl)(系统调用)

ioctl: 把控制信息传递给设备驱动程序。

库函数封装了系统调用,(系统调用影响系统性能,硬件对底层系统调用一次所能读写的数据块作出一定的限制),库函数提供输出缓冲功能。

 

open系统调用创建文件,设置的权限,(会减去umask设定的值,来确定文件最终的权限)

 

#include <unistd.h> 必须最早出现,因为它根据POSIX规范定义的标志可能会影响到其他的头文件。

 

查看程序运行时间:

TIMEFORMAT=”” time ./a.out

 

stat结构中

st_atime 文件上一次被访问的时间。

st_ctime 文件的权限/属主/组或内容上一次被改变的时间

st_mtime 文件内容上一次被修改的时间。

 

标准I/O库中,与底层文件描述符对应的对等物叫流(stream),被实现为指向结构FILE的指针。

 

printf %g 一般格式输出一个双精度浮点数。

%*s 10, “hello” -> | hello|

 

scanf %[ a-z] 读取一个字符集合

 

fdopen(...) 函数为一个已经打开的文件描述符提供stdio缓冲区

 

printdir.c 程序

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 || 
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

/*  Now we move onto the main function.  */

int main(int argc, char* argv[])
{
    char *topdir, pwd[2]=".";
    if (argc != 2)
        topdir=pwd;
    else
        topdir=argv[1];

    printf("Directory scan of %s\n",topdir);
    printdir(topdir,0);
    printf("done.\n");

    exit(0);
}

 

char *strerror(int errnum); 把错误编码映射为一个字符串

void perror(const char *s); 把error变量报告中的当前错误映射到一个字符串,并把它输出到标准错误输出流,格式为: s: ...…

 

/proc 包含许多特殊文件以允许对驱动和内核信息进行高层访问,如:

cat /proc/cpuinfo

 

fcntl系统调用对底层文件描述符提供了更多的控制方法。

mmap(内存映射), 程序如下:

/*  We start by defining a RECORD structure
    and then create NRECORDS versions each recording their number.
    These are appended to the file records.dat.  */

#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>

typedef struct {
    int integer;
    char string[24];
} RECORD;

#define NRECORDS (100)

int main()
{
    RECORD record, *mapped;
    int i, f;
    FILE *fp;

    fp = fopen("records.dat","w+");
    for(i=0; i<NRECORDS; i++) {
        record.integer = i;
        sprintf(record.string,"RECORD-%d",i);
        fwrite(&record,sizeof(record),1,fp);
    }
    fclose(fp);

/*  We now change the integer value of record 43 to 143
    and write this to the 43rd record's string.  */

    fp = fopen("records.dat","r+");
    fseek(fp,43*sizeof(record),SEEK_SET);
    fread(&record,sizeof(record),1,fp);

    record.integer = 143;
    sprintf(record.string,"RECORD-%d",record.integer);

    fseek(fp,43*sizeof(record),SEEK_SET);
    fwrite(&record,sizeof(record),1,fp);
    fclose(fp);

/*  We now map the records into memory
    and access the 43rd record in order to change the integer to 243
    (and update the record string), again using memory mapping.  */

    f = open("records.dat",O_RDWR);
    mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record), 
                          PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);

    mapped[43].integer = 243;
    sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);

    msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC);
    munmap((void *)mapped, NRECORDS*sizeof(record));
    close(f);

    exit(0);
}

原创:https://blog.csdn.net/ndzjx/article/details/88720502

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值