代码实现cp指令拷贝
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void main(int argc,char **argv)
{
int yuan;
int mubiao;
int size;
char *readbuf;
if(argc != 3)
{
printf("error!");
exit(-1);
}
yuan = open(argv[1],O_RDONLY);
size = lseek(yuan,0,SEEK_END);
lseek(yuan,0,SEEK_SET);
readbuf = (char *)malloc(sizeof(char)*size);
read(yuan,readbuf,sizeof(char)*size);
close(yuan);
lseek(mubiao,0,SEEK_SET);
mubiao = open(argv[2],O_WRONLY);
write(mubiao,readbuf,sizeof(char )*size);
close(mubiao);
}
代码实现更改文件内容
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void main(int argc,char **argv )
{
int mubiao;
int size;
char *readbuf;
if(argc != 2)
{
printf("error!");
exit(-1);
}
mubiao = open(argv[1],O_RDWR);
size = lseek(mubiao,0,SEEK_END);
lseek(mubiao,0,SEEK_SET);
readbuf = (char *)malloc(sizeof(char)*size);
read(mubiao,readbuf,size);
char *p = strstr(readbuf,"Li jian hua =");
if(p == NULL)
{
printf("NO found Li jian hua = ");
}
p = p +strlen("Li jian hua =");
*p = '6';
lseek(mubiao,0,SEEK_SET);
write(mubiao,readbuf,size);
close(mubiao);
}
输出结果:
Li jian hua =6