代码:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char *argv[])
{
if(argc < 1)
{
printf("no file \n");
return 0;
}
int fd = open(argv[1],O_RDONLY);
if(fd < 0)
{
perror("open ");
return 0;
}
long int size = lseek(fd,0,SEEK_END);
printf("%s size is %ld\n",argv[1],size);
return 0;
}
以上代码文件名为fsize.c
gcc fsize.c -o fsize
./fsize hello.c
在终端执行上述语句,即可获取hello.c文件的大小
可以通过 ll (即 ls -l)命令查看hello.c的文件大小是否和程序获取的一样来检验程序