1.思维导图:
2.作业
作业1、获取系统时间,并将系统时间写入文件中,实现现象
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
。。。
代码:
/*作业1、获取系统时间,并将系统时间写入文件中,实现现象
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 <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, const char *argv[])
{
//以可读可追加模式打开文件
FILE *fp;
if((fp = fopen("./time.txt","a+"))==NULL){
perror("open file");
return -1;
}
//定义变量
char oldtime[50]="";
char newtime[50]="";
char c[1024];
int line = 1;
while(1){
//得到系统时间串
time_t sys_time = time(NULL);//获取系统时间的毫秒数
struct tm *formattime = localtime(&sys_time);//将毫秒数转成时间结构体
sprintf(newtime,"%04d-%02d-%02d %02d:%02d:%02d",formattime->tm_year+1900,formattime->tm_mon+1,formattime->tm_mday,formattime->tm_hour,formattime->tm_min,formattime->tm_sec);//结构体转字符串
//得到行号
while((fgets(c,sizeof(c),fp))!=NULL)
{
if(c[strlen(c)-1]=='\n')
line++;
}
//比较旧串和新串是否一致,否则写入行号和新串
if(strcmp(oldtime,newtime)!=0){
fprintf(fp,"%d、%s\n",line,newtime);
fflush(fp);//手动更新文件缓存区
}
//更新旧串
strcpy(oldtime,newtime);
}
//关闭文件
fclose(fp);
return 0;
}
输出:
遇见问题,只有结束程序行号才更新
解决方法:
开头求一次行数,后面输出的时候line++就行
代码:
/*作业1、获取系统时间,并将系统时间写入文件中,实现现象
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 <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, const char *argv[])
{
//以可读可追加模式打开文件
FILE *fp;
if((fp = fopen("./time.txt","a+"))==NULL){
perror("open file");
return -1;
}
//定义变量
char oldtime[50]="";
char newtime[50]="";
char c[1024];
int line = 0;
//得到行号
while((fgets(c,sizeof(c),fp))!=NULL)
{
if(c[strlen(c)-1]=='\n')
line++;
}
while(1){
//得到系统时间串
time_t sys_time = time(NULL);//获取系统时间的毫秒数
struct tm *formattime = localtime(&sys_time);//将毫秒数转成时间结构体
sprintf(newtime,"%04d-%02d-%02d %02d:%02d:%02d",formattime->tm_year+1900,formattime->tm_mon+1,formattime->tm_mday,formattime->tm_hour,formattime->tm_min,formattime->tm_sec);//结构体转字符串
//比较旧串和新串是否一致,否则写入行号和新串
if(strcmp(oldtime,newtime)!=0){
fprintf(fp,"%d、%s\n",line++,newtime);
fflush(fp);//手动更新文件缓存区
}
//更新旧串
strcpy(oldtime,newtime);
}
//关闭文件
fclose(fp);
return 0;
}
输出:
作业2:使用fread和fwrite完成两个文件的拷贝
代码:
/*作业2:使用fread和fwrite完成两个文件的拷贝*/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
//判断传进来的文件个数是否为3
if(argc !=3 )
{
printf("file count error\n");
printf("usage: ./a.out srcfile destfile\n");
return -1;
}
//定义两个文件指针,分别指向源文件和目标文件
FILE *srcfp, *destfp;
//以只读的形式打开源文件
if((srcfp=fopen(argv[1], "r")) == NULL)
{
perror("srcfile open");
return -1;
}
//以只写的形式打开目标文件
if((destfp = fopen(argv[2], "w")) == NULL)
{
perror("destfile open");
return -1;
}
//定义字符搬运工
char c[1024];
while(!feof(srcfp))
{
//将字符写入到目标文件中去
if(fread(c,sizeof(char),sizeof(c),srcfp)==sizeof(c))
fwrite(c,sizeof(char),sizeof(c),destfp);
}
//关闭两个文件
fclose(srcfp);
fclose(destfp);
return 0;
}