文件IO函数

1.使用文件IO读取图片 文件大小、文件偏移量,宽度,高度 

#include <head.h>
int main(int argc, const char *argv[])
{
	int fd=open("/home/ubuntu/3.5/xiaoxin.bmp",O_RDONLY);
	if(fd==-1)
		PRINT_ERROR("open error");
	//大小
	int size;
	printf("fd=%d\n",fd);
	lseek(fd,2,SEEK_SET);
	if(read(fd,&size,sizeof(size))<=0)
	{
		PRINT_ERROR("read error");
		return -1;
	}
	printf("文件大小为%d\n",size);
	
	
	//文件偏移量
	lseek(fd,4,SEEK_CUR);
	int res;
	if(read(fd,&res,4)<=0)
	{
		PRINT_ERROR("read error");
	}
	printf("偏移量大小为%d\n",res);
 
	//宽
	lseek(fd,4,SEEK_CUR);
	int wide;
	read(fd,&wide,4);
	printf("文件宽为:%d\n",wide);
	//高
	lseek(fd,0,SEEK_CUR);
	int h;
	read(fd,&h,4);
	printf("文件高为:%d\n",h);
	close(fd);
	return 0;
}

 2.向一个程序中输入文件名,判断指定目录下是否有这个文件,如果有这个文件,

#include <head.h>
int main(int argc, const char *argv[])
{
	const char * dirname=argv[1];
	const char * filename=argv[2];
		
	DIR *d=opendir(dirname);
	if(d==NULL)
		PRINT_ERROR("opendir error");
    struct dirent *entry;
    struct stat file_stat;
    char file_path[1024];
 
   int found=0;
 
    while ((entry = readdir(d)) != NULL) 
	{
        if (strcmp(entry->d_name, filename) == 0) 
		{
            snprintf(file_path, sizeof(file_path), "%s/%s", dirname, filename);
 
            if (stat(file_path, &file_stat) == -1) 
			{
                PRINT_ERROR("获取文件信息失败");
            }
 
            printf("文件存在,属性信息如下:\n");
            printf("文件路径: %s\n", file_path);
            printf("文件大小: %ld 字节\n", file_stat.st_size);
            printf("文件权限: %o\n", file_stat.st_mode & 0777);
            printf("文件硬链接数: %ld\n", file_stat.st_nlink);
            struct tm *tm_info = localtime(&file_stat.st_mtime);
            if (tm_info == NULL)
    {
        PRINT_ERROR("localtime error");
    }
            printf("最后修改的时间:%d-%d-%d %d:%d:%d\n",tm_info->tm_year+1900,tm_info->tm_mon+1,tm_info->tm_mday,tm_info->tm_hour,tm_info->tm_min,tm_info->tm_sec);
            found = 1;
            break;
        }
    }
	if(!found)
	{
		printf("文件不存在\n");
	}
		if(closedir(d)==-1)
		PRINT_ERROR("closedir error");
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值