思维导图
作业1
#include<myhead.h>
void time_show(int line_number, struct tm *tm_ptr){ //终端显示时间
printf("%d、%4d-%02d-%02d %02d:%02d:%02d\n",\
line_number,\
tm_ptr->tm_year+1900,\
tm_ptr->tm_mon+1,\
tm_ptr->tm_mday,\
tm_ptr->tm_hour,\
tm_ptr->tm_min,\
tm_ptr->tm_sec);
}
void time_input(FILE *fp, int line_number, struct tm *tm_ptr){ //输出时间到文件
fprintf(fp, "%d、%4d-%02d-%02d %02d:%02d:%02d\n",\
line_number,\
tm_ptr->tm_year+1900,\
tm_ptr->tm_mon+1,\
tm_ptr->tm_mday,\
tm_ptr->tm_hour,\
tm_ptr->tm_min,\
tm_ptr->tm_sec);
}
int main(int argc, const char *argv[])
{
//定义文件
FILE *fp;
if((fp=fopen("./time.txt","a")) == NULL){
perror("fopen error\n");
return -1;
}
long cur = ftell(fp); 获取偏移量
int line_number = (int)(cur / 20) + 1; //计算行号
while(1){
//定义一个系统时间变量
time_t sysTime = time(NULL);
//定义时间结构体指针,执行时间
struct tm *tm_ptr = localtime(&sysTime);
fseek(fp, 0, SEEK_END);
time_show(line_number, tm_ptr);
time_input(fp, line_number, tm_ptr);
line_number++;
sleep(1);
}
fclose(fp);
return 0;
}
作业二
#include <stdio.h>
int main(int argc, const char *argv[]) {
//判断逻辑
if (argc != 3) {
printf("error\n");
printf("usage: ./a.out SourceFile DestinationFile\n");
return -1;
}
//定义源文件
FILE *srcfp;
if ((srcfp = fopen(argv[1], "r")) == NULL) {
perror("fopen error\n");
return -1;
}
//定义目标文件
FILE *desfp;
if ((desfp = fopen(argv[2], "w")) == NULL) {
perror("fopen error\n");
fclose(srcfp);
return -1;
}
char buf; //搬运工
size_t num; //fread返回值
while ((num = fread(&buf, 1, sizeof(buf), srcfp)) > 0) {
fwrite(&buf, 1, num, desfp);
}
// 关闭文件
fclose(srcfp);
fclose(desfp);
return 0;
}