lseek()函数详解

今日命令:
df -k //查看磁盘的分区情况

andrew@andrew-Thurley:~/work/iotec$ df -k
文件系统           1K-块     已用      可用 已用% 挂载点
udev             2800604        0   2800604    0% /dev
tmpfs             566068     8376    557692    2% /run
/dev/sda1      479605520 22543516 432676368    5% /
tmpfs            2830324    41372   2788952    2% /dev/shm
tmpfs               5120        4      5116    1% /run/lock
tmpfs            2830324        0   2830324    0% /sys/fs/cgroup
tmpfs             566068       68    566000    1% /run/user/1000

紧接着使用:sudo tune2fs -l /dev/sda1 命令查看洗盘下面的文件的具体划分

andrew@andrew-Thurley:~/work/iotec$ sudo tune2fs -l /dev/sda1
[sudo] andrew 的密码: 
tune2fs 1.42.13 (17-May-2015)
Filesystem volume name:   
Last mounted on:          /
Filesystem UUID:          7df6cde0-ecea-46a6-82e5-f477ec203ebe
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              30466048
Block count:              121846272
Reserved block count:     6092313
Free blocks:              114272179
Free inodes:              30176830
First block:              0
Block size:               4096  //说明一个磁盘块的大小是4096大小,使用IO读写一次大小是4096大小,所以 设置缓存的大小最好使用4096大小,用于存放文件,这样在频繁的使用读写文件的时候,能够将操作系统的性能发挥到最大化。
Fragment size:            4096
Reserved GDT blocks:      994
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Tue Aug  7 23:50:03 2018
Last mount time:          Fri Aug 10 20:47:28 2018
Last write time:          Fri Aug 10 20:47:24 2018
Mount count:              7
Maximum mount count:      -1
Last checked:             Tue Aug  7 23:50:03 2018
Check interval:           0 ()
Lifetime writes:          34 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
First orphan inode:       11411639
Default directory hash:   half_md4
Directory Hash Seed:      2707a476-4240-4ce1-99ef-36d47dd99c2a
Journal backup:           inode blocks

od -c //按照二进制文件查看文件

andrew@andrew-Thurley:~/work/iotec$ od -c hole.txt
0000000   0   1   2   3   4   5   6   7   8   9  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0   0   1   2   3   4   5   6   7   8   9

more hole.txt //按照字符查看文件 当碰到空洞文件的’\0’的时候会停止 ,因此对于空洞文件,hole.txt使用more将无法查看空洞后面的文件

andrew@andrew-Thurley:~/work/iotec$ od -c hole.txt
0000000   0   1   2   3   4   5   6   7   8   9  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0   0   1   2   3   4   5   6   7   8   9
头文件:#include  #include 
用 法: off_t lseek(int handle, off_t offset, int fromwhere);

EEK_SET 将读写位置指向文件头后再增加offset个位移量。
SEEK_CUR 以目前的读写位置往后增加offset个位移量。
SEEK_END 将读写位置指向文件尾后再增加offset个位移量。
当whence 值为SEEK_CUR 或SEEK_END时,参数offet允许负值的出现。
下列是较特别的使用方式:
1) 欲将读写位置移到文件开头时:
lseek(int fildes,0,SEEK_SET);
2) 欲将读写位置移到文件尾时:
lseek(int fildes,0,SEEK_END);
3) 想要取得目前文件位置时:
lseek(int fildes,0,SEEK_CUR);

返回值
当调用成功时则返回目前的读写位置,也就是距离文件开头多少个字节。若有错误则返回-1,errno 会存放错误代码。
可能设置erron的错误代码:
EBADF: fildes不是一个打开的文件描述符。
ESPIPE:文件描述符被分配到一个管道、套接字或FIFO。
EINVAL:whence取值不当。

例子:

#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
int handle;
char msg[]="This is a test";
char ch; 
/*create a file*/
handle=open("TEST.$$$",O_CREAT|O_RDWR,S_IREAD|S_IWRITE);
/*write some data to the file*/
write(handle,msg,strlen(msg));
/*seek to the begining of the file*/
lseek(handle,0,SEEK_SET);
/*reads chars from the file until we hit EOF*/
do
{
read(handle,&ch,1);
printf("%c",ch);
}while(!EOF);
close(handle);
return0;
}

代码例子2:

#define BUFFER_SIZE 1024
int main(int argc,char **argv)  
{  
    int  readfd, writefd;  
    long filelen=0;  
    int  ret=1;  
    char buffer[BUFFER_SIZE];  
    char *ptr;  

    /*打开源文件*/   
    if((readfd=open("test.txt", O_RDONLY|O_CREAT)) == -1)   
    {   
        printf("Open Error\n");   
        exit(1);   
    }   

      /*创建目的文件*/   
    if((writefd=open("dest.txt", O_WRONLY|O_CREAT)) == -1)   
    {   
        printf("Open Error\n");   
        exit(1);   
    }   

    /*测得文件大小*/  
    filelen= lseek(readfd,0L,SEEK_END);  
    lseek(readfd,0L,SEEK_SET);  
    printf("read file size is %d\n",filelen);  

    /*进行文件拷贝*/  
    while(ret)   
    {   
        ret= read(readfd, buffer, BUFFER_SIZE);  
        if(ret==-1)  
        {  
            printf("read Error\n");   
            exit(1);          
        }  
        write(writefd, buffer, ret);  
        filelen-=ret;  
        bzero(buffer,BUFFER_SIZE);  
    }   

    close(readfd);   
    close(writefd);   
    exit(0);   
}

你可能感兴趣的:(linux,嵌入式-Linux)