阅读背景:

Linux C程序练习(1)文件操作_u014717398的博客

来源:互联网 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>

#define BUFFERSIZE 1024

typedef int fid_t;

int main(int argc,char **argv){
    fid_t fd1=0;
    fid_t fd2=0;
    int readBytes=0;
    char *buffer=NULL;

    buffer=(char*)malloc(BUFFERSIZE*sizeof(char));
    memset(buffer,0,BUFFERSIZE*sizeof(char));
    
    fd1=open("/etc/passwd",O_RDONLY);
    if(fd1<0){
    	perror("open(fd1)");
    	exit(1);
    }
    
    fd2=open("passwd.bak",O_CREAT|O_RDWR|O_EXCL,0644);
    if(fd2<0){
    	perror("open(fd2)");
    	exit(1);
    }

	while((readBytes=read(fd1,buffer,BUFFERSIZE))>0){
		if(write(fd2,buffer,BUFFERSIZE)<0){
			perror("write(fd2)");
			exit(1);
		}
		memset(buffer,0,BUFFERSIZE*sizeof(char));
	}
	
	if(readBytes<0){
		perror("read(fd1)");
		exit(1);
	}
	close(fd1);
	close(fd2);
	exit(0);
}#include <stdio.h>
#include <stdlib.h>
#include



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: