1、利用文件系统调用函数实现cp命令,例如./a.outa.txtb.txt当前目录下的a.txt到b.txt#includestring.h#includestrings.h#includestdio.h#includestdlib.h#defineBUFFER_SIZE1024intmain(intargc,char**argv){FILE*from_fd;FILE*to_fd;longfile_len=0;charbuffer[BUFFER_SIZE];char*ptr;/*判断入参*/if(argc!=3){printf(Usage:%sfromfiletofile\n,argv[0]);exit(1);}/*打开源文件*/if((from_fd=fopen(argv[1],rb))==NULL){printf(Open%sError\n,argv[1]);exit(1);}/*创建目的文件*/if((to_fd=fopen(argv[2],wb))==NULL){printf(Open%sError\n,argv[2]);exit(1);}/*测得文件大小*/fseek(from_fd,0L,SEEK_END);file_len=ftell(from_fd);fseek(from_fd,0L,SEEK_SET);printf(fromfilesizeis=%d\n,file_len);/*进行文件拷贝*/while(!feof(from_fd)){fread(buffer,BUFFER_SIZE,1,from_fd);if(BUFFER_SIZE=file_len){fwrite(buffer,file_len,1,to_fd);}else{fwrite(buffer,BUFFER_SIZE,1,to_fd);file_len=file_len-BUFFER_SIZE;}//写入完成后清空缓冲区memset(buffer,0,BUFFER_SIZE);}fclose(from_fd);fclose(to_fd);exit(0);}2、通过leek函数,计算文件大小。#includesys/type.h#includeunistd.hoff_tlseek(intfd,off_tpos,intorigin);fd文件描述符originSEEK_CUR:若pos为0,返回当前文件位置(相对文件当前读写指针的位置+pos)SEEK_END:若pos为0,设为文件尾部(相对当前文件长度+pos)SEEK_SET:若pos为0,设文件开始(将文件设置成pos)Pos文件偏移量返回值:成功返回新文件位置,失败返回-1运行时,要输入要计算文件的名字,和一个复制用的文件名(可不存在的)#includestdio.h//printffprintf#includestring.h//memset#includeunistd.h//readwritelseek#includestdlib.h//exit#includeerrno.h//errno全局变量#includefcntl.h//open#defineLEN1024voidmycp(intfd_in,intfd_out){charbuff[LEN]={0};intsize;intlen;//如果读取成功,将缓冲区数据写入out文件while((size=read(fd_in,buff,sizeof(buff)))0){//写入数据,如果失败,程序退出if(write(fd_out,buff,size)!=size){fprintf(stderr,writeerror:%s\n,strerror(errno));exit(1);}//打印偏移量len=lseek(fd_in,0,SEEK_CUR);printf(读了%d\n,len);//写入完成后清空缓冲区memset(buff,0,sizeof(buff));}if(size0){fprintf(stderr,readerror:%s\n,strerror(errno));exit(1);}return;}intmain(intargc,char**argv){//判断参数是否输入正确(是否输入了两个文件名)if(argc3){fprintf(stderr,输入参数出错:%s\n,strerror(errno));exit(1);}//打开文件,判断打开是否失败(打开需计算的文件)intfd_in=open(argv[1],O_RDONLY);if(fd_in0){fprintf(stderr,打开文件出错:%s\n,strerror(errno));exit(1);}intfd_out=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);//(存放文件)if(fd_out0){fprintf(stderr,写入文件出错:%s\n,strerror(errno));exit(1);}//拷贝文件数据mycp(fd_in,fd_out);close(fd_in);close(fd_out);return0;}3、实现cat命令查看文件内容,例如./a.outa.txt可以打印a.txt内容#includeunistd.h#includesys/types.h#includeerrno.h#includestdio.h#includestring.h#includestdlib.h#includesys/stat.h#includefcntl.h#includeerrno.hvoidmain(intargc,char*argv[]){intfd;charbuffer[125];if((fd=open(argv[1],O_RDONLY))0){fprintf(stderr,error:%s\a\n,strerror(errno));exit(0);};printf(showfile:\n);while(1){memset(buffer,0,sizeof(buffer));if(read(fd,buffer,125)0){fprintf(stderr,error:%s\a\n,strerror(errno));exit(0);};if(buffer[99]==0)break;printf(%s,buffer);}close(fd);}4、编程实现两个进程同时对同一个文件每隔一秒钟写入一个字节的数据,对文件上锁以实现数据安全。#includestdio.h#includestring.h#includestdlib.h#includeerrno.h#includefcntl.h#includeunistd.h//上阻塞写锁#defineS_WRLKW(fd,start,whence,len)\lock_reg(fd,F_SETLKW,F_WRLCK,start,whence,len)//F_SETLKW(wait阻塞)F_WRLCK(独占性写锁)//解锁#defineUN_LOCK(fd,start,whence,len)\lock_reg(fd,F_SETLK,F_UNLCK,start,whence,len)//F_SETLK(非阻塞)F_UNLCK(解锁一个区域)voidsys_error(constchar*name,interr){fprintf(stderr,%s:%s\n,name,strerror(err));exit(1);}//文件锁操作intlock_reg(intfd,intflag,shorttype,off_tstart,shortwhence,off_tlen){//准备文件锁结构体structflockl;memset(&l,0,sizeof(l));//填入锁信息l.l_type=type;//type锁类型l.l_start=start;//start偏移量offsetl.l_whence=whence;//whence相对位置SEEK_SETSEEK_CURSEEK_ENDl.l_len=len;//len锁长度0表示从指定位置到文件尾部//上锁:fcntlif(fcntl(fd,flag,&l)0)return0;return1;}//pid1:./a.outa.txt12345//pid2:./a.outa.txtabcedintmain(intargc,char*argv[]){if(argc3){fprintf(stderr,usage:%sfilestrings\n,argv[0]);exit(1);}intfd=open(argv[1],O_CREAT|O_WRONLY,0664);if(fd0)sys_error(open,errno);intlen=strlen(argv[2]);inti=0;//对文件上锁(写,阻塞)intr=S_WRLKW(fd,0L,SEEK_SET,0);if(r==0)fprintf(stderr,s_wrlockerror\n);elseprintf(s_wrlocksuccess!\n);//每间隔1s写入一个数据lseek(fd,0L,SEEK_END);for(;ilen;i++){if(write(fd,&argv[2][i],1)!=1)sys_error(write,errno);printf(pid(%d)writesuccess!\n,getpid());//获取当前进程idsleep(1);}printf(allfiledatawritesuccess\n);//解锁UN_LOCK(fd,0L,SEEK_SET,0);close(fd);return0;}5、编程实现,利用存储映射函数将一文件映射的用户空间,然后读取信息并打印出来。#includestdio.h#includestring.h#includestdlib.h#includeerrno.h#includefcntl.h#includeunistd.h#includesys/mman.h#includeunistd.hvoidcopy(intfd_in,intfd_out);//添加、去除文件状态标志intset_fl(intfd,intflag);intclr_fl(intfd,intflag);voidsys_error(constchar*name,interr){fprintf(stderr,%s:%s\n,name,strerror(err));exit(1);}//./a.outa.txtintmain(intargc,char**argv){if(argc2){fprintf(stderr,usage:%sfile\n,argv[0]);exit(1);}//打开文件一定要和映射时读写方式对应//注意:打开文件必须要有读权限intfd=open(argv[1],O_RDWR);if(fd0)sys_error(open,errno);intlen=lseek(fd,0L,SEEK_END);//开始建立映射映射区可读、可写共享模式char*p=(char*)mmap(0,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);if(p==MAP_FAILED){//(void*)(-1)sys_error(mmap,errno);}//通过映射区查看文件内容printf(p:%s\n,p);//断开映射munmap(p,len);close(fd);return0;}6、利用库函数进行文件读写操作,新建一个学生信息结构体student,数据成员有名字,年龄,性别等,利用库函数把信息保存在文件stu.dat中,然后再读取