#include <stdio.h>
#include <stdlib.h>
//3个头文件
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
 
static  int flen(const char *fname)//static 表示禁止该函数外部扩展 ,返回文件大小 fname只读不写
{
	struct stat statres;
	if(stat(fname,&statres)<0){
		perror("state()");
		exit(1);
	}
	return statres.st_size;
}
 
int main(int argc,char **argv){
 
	if(argc<2){
		fprintf(stderr,"Usage...\n");
		exit(1);
	}
	printf("%d",flen(argv[1]));//要么用一个变量来接受,要么打印出来
 
exit(0);
}
 
 
  • off_t 类型的 st_size; 不知道位数 修改
    • static off_t flen(..);printf("%lld\n",flen(argv[1]));
    • 更好的方法 使用makefileCFLAGS+=-D_FILE_OFFSET_BITS=64
    • tags工具 类型 索引树;vim -t FILE/size_t
  • 所占磁盘的大小 由blksize和blk决定
#include <stdio.h>
 
#include <stdlib.h>
 
//3个头文件
 
#include<sys/types.h>
 
#include<sys/stat.h>
 
#include<unistd.h>
#include<fnctl.h>
int main(int argc, char **argv)
{
	  int fd;
	if(argc<2){
 
	fprintf(stderr,"Usage...\n");
 
	exit(1);
 
	}
	
	fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0600);
	if(fd<0){
		perror("open()");
		exit(1);
	}
	//空洞文件 5g 加上单位 防止溢出
	lseek(fd,5LL*1024LL*1024LL-1LL,SEEK_SET);
	/**error判断*/
	
	//一次系统调用
	write(fd,"",1);
	close(fd);
	exit(0);
}
 
 
  • 文件拷贝 cp /tmp/bigfile /tmp/bigfile.bck;stat /tmp/bigfile;inode文件标识;拷贝的文件不占磁盘空间,一块一块读看是否为空字符,一次write都没有执行;win与unix不一样 不是一个字节占一个?