文件I/O实现拷贝功能

#include
#include
#include
#include
#include

int main(int argc,char *argv[])
{
    if (argc < 3)
    {
        fprintf(stderr, "Usage: %s  \n",argv[0]);
        return -1;
    }
    int fin;
    if (0 > (fin = open(argv[1],O_RDONLY)))
    {
        perror("open filename1");
        return -1;
    }
    int fout;// 文件描述符
    if (0 > (fout = open(argv[2],O_WRONLY)))
    {
        perror("open filename2");
        return -1;
    }
    int ret;
    char buf[100];
    while(1)
    {
       ret = read(fin, buf, sizeof(buf));
        if (ret < 0)
        {
            perror("read");
            break;
        }
        else if (ret == 0)
        {
            printf("read file end\n");
            break;
        }
        if (ret != write(fout, buf, ret))
        {
            perror("write");
            break;
        }
    }
    close(fin);
    close(fout);
    return 0;
}

你可能感兴趣的:(C语言,文件II/O)