1> 使用fread和fwrite完成两个文件的拷贝
#include <myhead.h>
int main(int argc, const char *argv[])
{
//定义被复制文件指针
FILE *fp1=NULL;
if((fp1=fopen("./111.bmp","r"))==NULL)
{
perror("error open");
return -1;
}
//定义目标文件指针
FILE *fp2=NULL;
if((fp2=fopen("./333.bmp","w+"))==NULL)
{
perror("error open");
return -1;
}
//定义字符串数组存储读取的数据
char str[1024]="";
int res=0;
while((res=fread(str,1,sizeof(str),fp1))>0)
{
fwrite(str,1,res,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
2> 使用read、write完成两个文件的拷贝
#include <myhead.h>
int main(int argc, const char *argv[])
{
//定义被复制文件标识符
int fd1=-1;
if((fd1=open("./111.bmp",O_RDONLY))==-1)
{
perror("open error");
return -1;
}
//定义目标文件标识符
int fd2=-1;
if((fd2=open("./222.bmp",O_RDWR|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open error");
return -1;
}
//定义数组存储读取的数据
char str[1024]="";
int t;
while((t=read(fd1,str,sizeof(str)))>0)
{
write(fd2,str,t);//向目标文件写入
}
//关闭文件
close(fd1);
close(fd2);
return 0;
}
3> 将时间在文件中跑起来
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./Time.txt","w+"))==NULL)
{
perror("open error");
return -1;
}
int n=1;
char T[1024]="";
char Tr[1024]="";
while(1)
{
sleep(1);
time_t sysTime=time(NULL);
struct tm *t=localtime(&sysTime);
fseek(fp,0,SEEK_SET);
snprintf(T,sizeof(T),"%d:%d-%d-%d %d:%d:%d\n",n,t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
fwrite(T,strlen(T),1,fp);
fseek(fp,0,SEEK_SET);
fread(Tr,strlen(T),1,fp);
printf("%s",Tr);
n++;
}
fclose(fp);
return 0;
}