2024.2.19
使用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;
}
使用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;
}
将时间在文件中跑起来