大白菜的linux之旅,文件操作首探
程序代码:
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAXLEN 100 void charge(char argv[],int n,int key) { int i; for(i=0;i<n;i++) argv[i]=argv[i]+key; } void xxx(int fd,int fd1,char buff[],int maxlen,int key) { int n; while((n=read(fd1,buff,maxlen))>0){ if(n!=maxlen){ charge(buff,n,key); if(write(fd,buff,n)!=n){ printf("xie ru shuju shibai"); exit(0); } } else{ charge(buff,maxlen,key); if(write(fd,buff,maxlen)!=maxlen){ printf("xie ru shuju shibai"); exit(0); } } if(n<0) printf("du shibai"); } } int main() { int fd; int fd1; int fd2; char buff[MAXLEN]; int n,keyback=-16,key=16; if((fd=open("kle.txt",O_RDWR))==-1) printf("open fail\n"); if((fd1=open("readme.txt",O_RDONLY))==-1) printf("readme.txt open faile!!\n"); if((fd2=open("mmt.txt",O_RDWR))==-1) printf("open fail\n"); xxx(fd,fd1,buff,MAXLEN,key); lseek(fd,0,SEEK_SET); xxx(fd2,fd,buff,MAXLEN,keyback); printf("xieruchenggong\n"); exit(0); }
[ 本帖最后由 朱三哥 于 2013-1-2 23:49 编辑 ]