文件IO函数

#include 
#include 
int main(int argc, const char *argv[])
{
	//打开文件
	int fd=open("open.txt",O_RDONLY);
	if(fd<0)
	{
		ERR_MSG("open");
		return -1;
	}
	printf("打开成功\n");

//	off_t size = lseek(fd,0,SEEK_END);
//	printf("size=%ld\n",size);

	char c;
	ssize_t size=0;
	int count=0;
	while(1)
	{
		size=read(fd,&c,1);
		if(0 == size)
		{
			break;
		}
		count++;
	}
	printf("count=%d\n",count);




	//关闭文件
	if(close(fd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	printf("关闭成功\n");
	return 0;
}
#include 
#include 
void get_filePermission(mode_t m)
{
	int a[9]={0400,0200,0100,0040,0020,0010,0004,0002,0001};
	char s[9]={'x','w','r','x','w','r','x','w','r'};
	for(int i=0;i<9;i++)
	{
		if((a[i]&m) == 0)
			putchar('-');
		else
			printf("%c",s[8-i]);	
	}
}
int main(int argc, const char *argv[])
{

	struct stat buf;
	if(stat("time.c",&buf)<0)
	{
		ERR_MSG("stat");
		return -1;
	}
	//文件的类型和权限
	printf("mode: 0%o\n",buf.st_mode);
	get_filePermission(buf.st_mode);

	//文件的硬连接数
	printf("link: %ld\n",buf.st_nlink);

	//文件的所属用户
	printf("uid: %d\n",buf.st_uid);

	//文件所属组用户
	printf("gid: %d\n",buf.st_gid);

	//文件大小
	printf("size: %ld\n",buf.st_size);

	//文件的修改时间
	printf("time: %ld\n",buf.st_ctime);
	
	return 0;
}

 

你可能感兴趣的:(linux,算法,c语言)