时间
#include <myhead.h>
int main(int argc, const char *argv[])
{
//定义一个系统时间变量
//time_t sysTime = time(NULL);
//定义时间结构体指针,执行时间
//struct tm *tm_ptr = localtime(&sysTime);
FILE *fp;
if((fp=fopen("./03day3.txt","w+"))==NULL)
{
perror("fopen perror");
return -1;
}
while(1)
{
int c=0; //显示序号
char buf;
fseek(fp,0,SEEK_SET);
while((buf=fgetc(fp))!=EOF)
{
if(buf=='\n')
{
c++;
}
}
printf("%d\n",c);
time_t sysTime = time(NULL);
struct tm *tm_ptr = localtime(&sysTime);
fseek(fp,0,SEEK_END);
fprintf(fp,"%d、%4d-%2d-%2d %02d:%02d:%02d\n",\
c,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);
usleep(1000000);
sysTime+=1;
}
//关闭文件指针
fclose(fp);
return 0;
}
使用fread、fwrite完成两个文件的拷贝
#include <myhead.h>
int main(int argc, const char *argv[])
{
//定义一个只写文件指针
FILE *fp;
/*if((fp=fopen("./01day3.txt","w"))==NULL)
{
perror("fopen error");
return -1;
}
int a=1; //控制循环次数
while(a<4)
{
fprintf(fp,"%s","hello world\n");
a++;
}*/
//将上一个文件改为只读
if((fp=fopen("./01day3.txt","r"))==NULL)
{
perror("fopen error");
return -1;
}
//定义一个只写文件指针
FILE *fq;
if((fq=fopen("./02day3.txt","w"))==NULL)
{
perror("fopen error");
return -1;
}
//将光标定位到结尾
fseek(fp,0,SEEK_END);
int t=ftell(fp); //求文件大小
fseek(fp,0,SEEK_SET); //将光标重新定位到开头
char arr[t-1];
while((fread(arr,sizeof(arr),1,fp))!=0)
{
fwrite(arr,sizeof(arr),1,fq);
}
//关闭文件1和文件2
fclose(fp);
fclose(fq);
return 0;
}
思维导图