1.使用fread和fwrite完成两个文件的拷贝
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, const char *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./tset.txt","w"))==NULL)
{
perror("open error");
return -1;
}
char str[20]="";
puts("please input:");
fgets(str,sizeof(str),stdin);//从终端获取字符串
str[strlen(str)-1]=0;
fwrite(str,1,strlen(str),fp);
fclose(fp);
//以读的形式再次打开
if((fp=fopen("./tset.txt","r"))==NULL)
{
perror("open error");
return -1;
}
char str1[20]="";
fread(str1,1,sizeof(str1),fp);
printf("%s",str1);
FILE *fp2=NULL;
if((fp2=fopen("./test1.txt","w"))==NULL)
{
perror("open error");
return -1;
}
fwrite(str1,1,strlen(str1),fp2);
return 0;
}
效果显示:
2.使用read、write完成两个文件的拷贝
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int fd=-1;
if((fd=open("./21.txt",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open error");
return -1;
}
char str[20]="";
puts("input:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
write(fd,str,sizeof(str));
close(fd);
if((fd=open("./21.txt",O_RDONLY))==-1)
{
perror("open error");
return -1;
}
char str1[20]="";
int res=read(fd,str1,sizeof(str1));
write(1,str1,res);
close(fd);
int fd2=-1;
if((fd2=open("./22.txt",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open error");
return -1;
}
write(fd2,str1,sizeof(str));
close(fd2);
return 0;
}
效果显示:
3.用时间处理函数让时间在终端跑起来
#include<myhead.h>
int main(int argc, const char *argv[])
{
while(1)
{
//获取时间的秒数
time_t sysTime=time(NULL);
//通过秒数获取时间结构体指针
struct tm *t=localtime(&sysTime);
//将时间打印在终端上
printf("%4d-%2d-%2d %2d:%2d:%2d\n",t->tm_year+1900,\
t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
fflush(stdout);
sleep(1);
}
return 0;
}