两个文件copy
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
if (argc != 3) //传参小于3失败
{
printf("error");
printf("./a.out scrfile destfile");
}
FILE * scr;
if ((scr = fopen(argv[1],"a+")) == NULL )
{
perror("error\n");
return -1;
}
FILE * dest;
if ((dest = fopen(argv[2],"a+")) == NULL )
{
perror("error\n");
return -1;
}
char arr[20];
fread(arr,1,sizeof(arr),scr);
printf("arr=%s\n",arr);
fwrite(arr,1,strlen(arr),dest);
fscanf(dest,"%s",arr);
fclose(scr);
fclose(dest);
printf("%s",arr);
return 0;
}
1、2023-5-25 14:32:50
2、2023-5-25 14:32:51
3、2023-5-25 14:32:52
//按下ctrl+c,再重新运行程序后
4、2023-5-25 14:33:20
5、2023-5-25 14:33:21
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, const char *argv[])
{ char new[50] = "";
char old[50] = "";
FILE *fp;
if ((fp = fopen("./time.txt","a+")) == NULL)
{
perror("file error\n");
return -1;
}
int line = 0;
while (1)
{ time_t clock = time(NULL);
struct tm * fomattime = localtime(&clock);
sprintf(new,"%4d-%2d-%2d %2d:%2d:%2d", fomattime->tm_year+1900\
,fomattime->tm_mon+1,\
fomattime->tm_mday,\
fomattime->tm_hour,\
fomattime->tm_min,\
fomattime->tm_sec);
if (strcmp(new,old) != 0)
{
fprintf(fp,"%d: %s\n",line++,new);
fflush(fp);
}
strcpy(old,new);
}
fclose(fp);
return 0;
}