思维导图
作业1、获取系统时间,并将系统时间写入文件中,
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fp=NULL;
char str1[50]={0};
char str2[50]={0};
int count=0;
int i=0;
if((fp=fopen("./6.txt","a"))==NULL)
{
perror("open file");
return -1;
}
while(1)
{
time_t sys_time=time(NULL);
struct tm *time11=localtime(&sys_time);
sprintf(str1,"%4d-%2d-%2d,%2d:%2d:%2d\n",
time11->tm_year+1900,\
time11->tm_mon+1,\
time11->tm_mday,\
time11->tm_hour,\
time11->tm_min,\
time11->tm_sec);
if(strcmp(str1,str2)!=0)
{
fprintf(fp,"[%d]:%s\n",++count,str1);
strcpy(str2,str1);
i++;
if(i==20)break;
}
}
fclose(fp);
return 0;
}
作业2:使用fread和fwrite完成两个文件的拷贝
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
FILE *fpw,*fpr;//定义写读指针
//读指针指向被拷贝文件
if((fpr=fopen("./1.c","r"))==NULL)
{
perror("open rfile");
return -1;
}
//写指针指向拷贝文件
if((fpw=fopen("./5.txt","w"))==NULL)
{
perror("open wfile");
return -1;
}
int ret;
char str[100]={0};
//拷贝文件
while( (ret=fread(str,sizeof(char),1,fpr) !=0) )
{
fwrite(str,sizeof(char),1,fpw);
}
return 0;
}