01.思维导图
02.将当前的时间写入到time. txt的文件中,如果ctrl+c退出之后,在再次执行支持断点续写
1.2022-04-26 19:10:20
2.2022-04-26 19:10:21
3.2022-04-26 19:10:22
//按下ctrl+c停止,再次执行程序
4.2022-04-26 20:00:00
5.2022-04-26 20:00:01
#include <25051head.h>
//1.封装时间函数
int get_currenttime(struct tm *s)
{time_t t;//1.计算秒数,存到t变量time(&t);//printf("t=%ld\n",t);//2.转换年月日时分秒struct tm *temp=localtime(&t);if(NULL==temp){ERRLOG("localtime_error");return -1;}//将时间信息复制到传入结构体中*s=*temp;//成功
#if 0printf("%d-%02d-%2d %02d:%02d:%02d\n",\s->tm_year+1900,s->tm_mon+1,\s->tm_mday,s->tm_hour,s->tm_min,s->tm_sec);
#endifreturn 0;
}
//2.封装获取行号函数
int get_linenum(FILE* fp)
{int line_count=0;//记录当前文件指针位置long current_pos=ftell(fp);//将文件指针移动到文件开头fseek(fp,0,SEEK_SET);char ch;while((ch=fgetc(fp))!=EOF){if(ch=='\n'){line_count++;}}//恢复指针的位置fseek(fp,current_pos,SEEK_SET);return line_count+1;
}
int main(int argc, const char *argv[])
{//1.打开文件FILE* fp=fopen("./mytime.txt","a+");if(NULL==fp){ERRLOG("fopen_error");return -1;}char buf[128]="";//char last_line[128]="";while(1){//获取行号int line_number=get_linenum(fp);//获取时间结构体struct tm time_info;if(get_currenttime(&time_info)!=0){printf("get_currenttime error");fclose(fp);return -1;}//2.写文件snprintf(buf,sizeof(buf)-1,"%d:%d-%02d-%02d %02d:%02d:%02d\n",\line_number,time_info.tm_year+1900,time_info.tm_mon+1,\time_info.tm_mday,time_info.tm_hour,time_info.tm_min,time_info.tm_sec);size_t res=fwrite(buf,1,strlen(buf),fp);if(res<strlen(buf)){printf("fwrite_error");fclose(fp);return -1;}//偏移光标fseek(fp,0,SEEK_SET);//3.读文件
#if 0while(1){//清零 memset(buf,0,sizeof(buf));res=fread(buf,1,sizeof(buf)-1,fp);if(res>0){fprintf(stdout,"%s",buf);}if(feof(fp)){//printf("读取到文件结尾..\n");break;}if(ferror(fp)){printf("fread文件读取失败.\n");break;}}
#endiffprintf(stdout,"%s",buf);sleep(1);} //4.关闭文件fclose(fp);fp=NULL;return 0;
}
#ifndef __25051HED_H__
#define __25051HED_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>//引入open函数
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>//引入 getdtablesize函数,获取文件描述符个数,包含close函数
#include <time.h>#define ERRLOG(msg) do{printf("__%d__",__LINE__);fflush(stdout);perror(msg);return -1;}while(0)
#endif
03.使用文件IO函数实现图片的拷贝
#include <25051head.h>
int main(int argc, const char *argv[])
{//1.打开文件umask(0);//打开当前目录下的0001.jpg文件,以w方式打开int src_fd=open("./0001.jpg",O_RDONLY);if(-1==src_fd){ERRLOG("opensrc_error");}printf("src_fd=%d\n",src_fd);//打开目标图片文件,以读写、创建、清零模式打开int dest_fd=open("./0002.jpg",O_RDWR|O_CREAT|O_TRUNC,0777);if(-1==dest_fd){ERRLOG("opendest_error");close(src_fd);}printf("dest_fd=%d\n",dest_fd);printf("opendest_success..\n");//2.拷贝数据char buf[128];ssize_t ret;while(1){//从源数据读取数据ret=read(src_fd,buf,sizeof(buf)-1);if(-1==ret){ERRLOG("read_error");}else if(0==ret){printf("end of source_image..\n");break;}//将读取的数据写入目标文件ssize_t write_ret=write(dest_fd,buf,ret);if(-1==write_ret){ERRLOG("write_error");}else if(write_ret!=ret){fprintf(stderr,"写入的超过读取的\n");break;}}//3.关闭文件if(-1==close(src_fd)){ERRLOG("关闭源文件失败");}if(-1==close(dest_fd)){ERRLOG("关闭目标文件失败");}printf("close_success..\n");return 0;
}
04.使用文件IO读取图片 文件大小、文件偏移量,宽度,高度,像素
1.bmp文件头(bmp file header):提供文件的格式、大小等信息 (14字节)
2.位图信息头(bitmap information):提供图像数据的尺寸、位平面数、压缩方式、颜色索引等信息(50字节)
3.位图数据(bitmap data):就是图像数据啦
#include <25051head.h>
int main(int argc, const char *argv[])
{//1.打开文件//umask(0);//2.以只读的方式打开图片文件int fd=open("./123.bmp",O_RDONLY);if(-1==fd){ERRLOG("open_error");}//1.获取123.bmp文件的大小int buf;off_t size=lseek(fd,2,SEEK_SET);read(fd,&buf,4);printf("图片123.bmp的大小为:%d\n",buf);//2.获取文件偏移量//偏移光标到起始位置lseek(fd,10,SEEK_SET);read(fd,&buf,4);printf("图片123.bmp的文件偏移量为%d\n",buf);//3.获取宽度lseek(fd,18,SEEK_SET);read(fd,&buf,4);printf("图片123.bmp的宽度为%d\n",buf);//4.获取宽度lseek(fd,22,SEEK_SET);read(fd,&buf,4);printf("图片123.bmp的高度为%d\n",buf);//5.获取图片像素if(-1==close(fd)){ERRLOG("close_error");}return 0;
}