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;
}