ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20170925]建立文件分配大小.txt

[20170925]建立文件分配大小.txt

原创 Linux操作系统 作者:lfree 时间:2017-09-25 21:06:38 0 删除 编辑
[20170925]建立文件分配大小.txt

--//有时候工作需要建立一个文件.一般在linux下使用dd.总结一下其他方法:

1.方法1:

$ cat a.c

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/io.h>
#include <stdio.h>
int main()
{
    FILE* file = fopen ("test", "w+");
    fseek (file, 64, SEEK_SET);
    fprintf (file, "x");
    fclose (file);
    return 0;
}

$ gcc a.c
$ ./a.out

$ xxd -c 16 test
0000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000040: 78                                       x

--//偏移从0记数,这样建立的文件大小是65字节.

2.方法2:.fallocate

--//实际上linux下有一个命令建立与分配文件大小.fallocate命令.我仅仅知道centos6.X版本有这个命令.
--//按照man文档的提示:内核版本是2.6.31.

# man fallocate

FALLOCATE(1)                                                      FALLOCATE(1)

NAME
       fallocate - preallocate space to a file.

SYNOPSIS
       fallocate [-n] [-o offset] -l length filename

DESCRIPTION
       fallocate  is  used to preallocate blocks to a file.  For filesystems which support the fallocate system call,
       this is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks.
       This is much faster than creating a file by filling it with zeros.

       As of the Linux Kernel v2.6.31, the fallocate system call is supported by the btrfs, ext4, ocfs2, and xfs
       filesystems.

       The exit code returned by fallocate is 0 on success and 1 on failure.

#  fallocate -l 10 a
#  ls -l a
-rw-r--r-- 1 root root 10 2017-09-25 15:23:28 a

3.windows下:

--//windows下:
R:\>fsutil file createnew test.out 64
已创建文件 R:\test.out

R:\>dir test.out
 驱动器 R 中的卷是 RAMDISK
 卷的序列号是 0122-14E0

 R:\ 的目录

2017/09/25  15:21                64 test.out
               1 个文件             64 字节
               0 个目录    652,371,968 可用字节

R:\>d:\tools\Vim\vim74\xxd.exe -c 16 test.out
0000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2145422/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7969645