linux C语言拷贝文件源码


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
 
 
int main()
{
    char c;
    int in;
    int out;
    in = open("test.txt",O_RDONLY);
    out = open("out.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
    while(read(in,&c,1)==1){
    write(out,&c,1);
    }
    return 0;
}


改良后拷贝代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>


int main()
{
    char block[1024];
    int in;
    int out;
    int nread;
    in = open("test.txt",O_RDONLY);
    out = open("out.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
    while(( nread = read(in,block,sizeof(block)))>0) {
    	write(out,block,nread);
    }
    return 0;
}



你可能感兴趣的:(linux)