C/C++产生空洞文件(lseek)

本次实验是使用lseek函数实现一个空洞为文件,所产生的文件开头是ABCDE结尾是hello中间32个字符是‘\0’填充。

实验环境为阿里云ubuntu16.04 编译器是gcc5.4版本。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ERR_EXIT(m)			\
	do						\
	{						\
		perror(m);			\
		exit(EXIT_FAILURE);	\
	}while(0)

int main()
{
	int fd = open("test.txt",O_WRONLY | O_CREAT | O_TRUNC);
	if(fd == -1)
	{
		ERR_EXIT("open error");
	}
	write(fd,"ABCDE",5);
	int ret = lseek(fd,32,SEEK_CUR);
	if(ret == -1)
	{
		ERR_EXIT("lseek error");
	}
	write(fd,"hello",5);
	close(fd);
	return 0;
}

如何和查询test.txt文件大小?

ls -l test.txt

有42个字符ABCDE hello 10个字符32个‘\0’字符。 

du -h test.txt  查询 

大小为4K因为系统分配文件最小为4K大小。

使用cat test.txt查看文件内容

显示只有字符没有‘\0’。因为cat不能查看完全。

使用 od -c test.txt  命令可以查看完全文件

 

你可能感兴趣的:(linux,C,linux系统编程)