使用fread和fwrite完成兩個圖片文件的拷貝
#include <myhead.h>
#define high 541
#define wide 541
int main(int argc, const char *argv[])
{
//以只讀的方式打開圖片文件1.bmp
FILE *fp = NULL;
if((fp = fopen("./1.bmp", "r")) == NULL)
{
perror("fopen error");
return -1;
}
//以只寫的方式打開文件work1.bmp
FILE *fq=NULL;
if((fq= fopen("./work1.bmp", "w")) == NULL)
{
perror("fopen error");
return -1;
}
//先復制位圖文件頭和位圖信息圖
unsigned char head[54];
fread(head,1,sizeof(head),fp);
fwrite(head,1,sizeof(head),fq);
//將光標定位在圖像像素矩陣位置
fseek(fp,54,SEEK_SET);
//將光標定位在圖像像素矩陣位置
fseek(fq,54,SEEK_SET);
unsigned char str[3]={0,0,0};//定義一個容器用來儲存和讀取像素
//循環(huán)取出1.bmp的像素存入work1.bmp中
for(int i=0;i<high;i++)
{
for(int j=0;j<wide;j++)
{
fread(str,1,sizeof(str),fp);//讀取像素
fwrite(str,1,sizeof(str),fq);//寫入像素
}
}
fclose(fp);
fclose(fq);
return 0;
}
現(xiàn)象展示:?
使用read、write完成兩個圖片文件的拷貝
#include <myhead.h>
#define high 653
#define wide 653
int main(int argc, const char *argv[])
{
//以只讀的方式打開圖片文件2.bmp
int fp=-1;
if((fp=open("./2.bmp",O_RDONLY))==-1)
{
perror("open error");
return -1;
}
//以只寫的方式打開文件work2.bmp
int fq=-1;
if((fq=open("./work2.bmp",O_WRONLY|O_CREAT|O_TRUNC,0666))==-1)
{
perror("fopen error");
return -1;
}
//先復制位圖文件頭和位圖信息圖
unsigned char head[54];
read(fp,head,sizeof(head));
write(fq,head,sizeof(head));
//將光標定位在圖像像素矩陣位置
lseek(fp,54,SEEK_SET);
//將光標定位在圖像像素矩陣位置
lseek(fq,54,SEEK_SET);
unsigned char str[3]={0,0,0};//定義一個容器用來儲存和讀取像素
//循環(huán)取出2.bmp的像素存入work2.bmp中
for(int i=0;i<high;i++)
{
for(int j=0;j<wide;j++)
{
read(fp,str,sizeof(str));//讀取像素
write(fq,str,sizeof(str));//寫入像素
}
}
close(fp);
close(fq);
return 0;
}
效果圖
?
3> 將時間在文件中跑起來
1、17:30:41
2、17:30:42
3、17:30:43
鍵入ctrl+c,結束進程后
...
4、17:35:28
5、17:35:29
#include <myhead.h>
#include <time.h>
int main(int argc, const char *argv[])
{ //打開時間文本文件
int fp = -1;
if((fp = open("time.txt", O_WRONLY|O_APPEND|O_CREAT)) == -1)
{
perror("open error");
return -1;
}
char time_buf[100]="";
int i=1;
while (1)
{
time_t sysTime=time(NULL);//獲取時間秒數(shù)
struct tm *t=localtime(&sysTime);//通過秒數(shù)獲取時間結構體指針
//將時間的格式串儲存到一個數(shù)組中
snprintf(time_buf, sizeof(time_buf), "%2d、%2d:%2d:%2d\n", i++,t->tm_hour, t->tm_min, t->tm_sec);
//寫入行號和時間串
write(fp, time_buf, strlen(time_buf));
printf("%s\n", time_buf);
sleep(1);
}
close(fp);
return 0;
}
效果圖:
?思維導圖文章來源:http://www.zghlxwxcb.cn/news/detail-827898.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-827898.html
到了這里,關于IO進程線程作業(yè)day2的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!