1>
要求:
源代码:
#include <myhead.h>
#define MAXSIZE 64
int main(int argc, char const *argv[])
{FILE *src = NULL;FILE *dest = NULL;if ((src = fopen("./hbao.bmp", "r")) == NULL){perror("fopen error");return -1;}if ((dest = fopen("./t1.bmp", "w")) == NULL){perror("fopen error");return -1;}char *buf[MAXSIZE];int res = 0;while ((res = fread(buf, 1, sizeof(buf), src)) != 0){fwrite(buf, 1, res, dest);}fclose(src);fclose(dest);return 0;
}
效果图
2>
要求:
源代码:
#include <myhead.h>
#define MAXSIZE 64
int main(int argc, char const *argv[])
{int fd1 = -1;int fd2 = -1;//umask(0000);if ((fd1 = open("./hbao2.bmp", O_RDONLY)) == -1){perror("open error");return -1;}if ((fd2 = open("./t2.bmp", O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1){perror("open error");return -1;}char buf[MAXSIZE];int res = 0;while ((res = read(fd1, buf, sizeof(buf))) != 0){write(fd2,buf,sizeof(buf));}close(fd1);close(fd2);return 0;
}
效果图:
3>
要求:
源代码:
#include <myhead.h>
int main(int argc, const char *argv[])
{FILE *fp = NULL; //文件操作char buf_time[100] = ""; //时间存储数组char line[100] = ""; //行数组char buf[100] = ""; //输出读取while (1){//以追加可读的方式打开文件if ((fp = fopen("./systime.txt", "a+")) == NULL){perror("fopen error");return -1;}//1、获取时间的秒数time_t sysTime = time(NULL);//2、通过秒数获取时间结构体指针struct tm *t = localtime(&sysTime);//循环查找最后一行的行号int i = 1;while (fgets(line, sizeof(line), fp) != NULL){i++;}//将元素放到数组中snprintf(buf_time, sizeof(buf_time), "%d,%2d:%2d:%2d\n", i, t->tm_hour, t->tm_min, t->tm_sec);//将数组写入到文件中fwrite(buf_time, strlen(buf_time), 1, fp);//移动光标至读取时间位置fseek(fp, -strlen(buf_time), SEEK_END);//输出读取的数据fread(buf, strlen(buf_time), 1, fp);printf("%s\n", buf);sleep(1);fclose(fp);}return 0;
}
效果图: