- 论坛徽章:
- 0
|
关于创建稀疏文件,昨天刚写了一个,拿来用下吧!
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6
7 #define SIZE (100 * 1024 * 1024) //100M
8 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH) //mode 744
9
10 int
11 main(int argc, char *argv[])
12 {
13 int fd;
14 char *buf = "\n";
15
16 if (argc != 2) {
17 fprintf(stderr, "Usage: %s file\n", argv[0]);
18 exit(1);
19 }
20 if ((fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, MODE)) < 0) {
21 fprintf(stderr, "open %s error!\n", argv[1]);
22 exit(1);
23 }
24 if (lseek(fd, SIZE, SEEK_SET) < 0) {
25 fprintf(stderr, "lseek %s error!\n", argv[1]);
26 exit(1);
27 }
28 if (write(fd, buf, sizeof(buf)) < 0) {
29 fprintf(stderr, "write %s error!\n", argv[1]);
30 exit(1);
31 }
32
33 exit(0);
34 } |
|