实现cp功能
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlb.h>
int main(int argc,char **argv){
int fdSrc;
int fdDes;
char *readbuf=NULL
if(argc!=3){
printf("not success");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readbuf=(char *)malloc(sizeof(char)*size+8);
int n_read=read(fdSrc,readbuf,size);
fdDes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write=write(fdDes,readbuf,strlen(readbuf));
close(fdSrc);
close(fdDes);
return 0
}
修改文件里的数据
文件打开读后写前
char *p=strstr(readbuf,"str");
if(p==NULL){
printf("failed");
exit(-1);
}
p=p+strlen(str);
*p='x';