概述
mycopy实例重写:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fsrc;
int fdst;
char buffer[1024]={0};
if(argc!=3)
{
printf("command is not foundn");
return 0;
}
fsrc = open(argv[1],O_RDWR);
lseek(fsrc,0,SEEK_SET);
read(fsrc,buffer,1024);
fdst = open(argv[2],O_RDWR|O_CREAT,0600);
write(fdst,buffer,sizeof(buffer));
close(fsrc);
close(fdst);
return 0;
}
【运行结果】
lwb@ubuntu:~/fileoperate$ gcc mycopy.c -o mycopy
lwb@ubuntu:~/fileoperate$ ./mycopy hello.c hellotest.c (同一目录下拷贝文件成功)
lseek函数实例:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
int main()
{
char *buf="abcdefg";
char *buf1=NULL;
int i=0;
int fd=creat("./lseektest.txt",0700);
if(fd<0)
{
printf("file create is faild;");
}
open("./lseektest.txt",O_RDWR);
write(fd,buf,8);
close(fd);
int fd1=open("./lseektest.txt",O_RDWR);
lseek(fd1,3,SEEK_SET);
lseek(fd1,-2,SEEK_CUR);
buf1=(char *)malloc(8);
memset(buf1,0,8);
read(fd1,buf1,3);
printf("testfile current char is: ");
while(buf1[i]!='