使用lseek的相反顺序C复制文件 [英] c copy files in reverse order using lseek

查看:229
本文介绍了使用lseek的相反顺序C复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得如何将一个文件复制到另一个从开始,但我怎么能修改程序,以它以相反的顺序复制?源文件应具有读取权限和目标文件读写执行。我不得不使用文件控件库。

例如:

 文件的文件B要
| --------- | | ---------- |
| ABCDEF | | FEDCBA |
| --------- | | ---------- |

*********************UPDATE**********

感谢您,MikeNakis的提示和建议
           ,Sangeeth您code

我修改了code,现在是按相反的顺序打印文件大小复制字节

这里是code

 #包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / stat.h>
#包括LT&;&unistd.h中GT;INT主(INT ARGC,CHAR *的argv []){    INT来源,DEST,N;
    焦炭BUF;
    INT文件大小;
    INT I;    如果(argc个!= 3){
        fprintf中(标准错误,使用率%S<信源><&DEST GT;,的argv [0]);
        出口(-1);
    }    如果((来源=打开(的argv [1],0400))小于0){//读取权限的用户源
        fprintf中(标准错误,不能开源);
        出口(-1);
    }    如果((目标=穿心莲(的argv [2],0700))℃下),用于用户对目标寄存器{// RWX权限
        fprintf中(标准错误,无法创建DEST);
        出口(-1);
    }    文件大小= lseek的(源,(off_t)0,SEEK_END); //文件大小是lastby +偏移
    的printf(源文件大小为%d \\ n,文件大小);    对于(I =文件大小 - 1; I> = 0;我 - ){//从结束字节读字节
        lseek的(源,(off_t)我,SEEK_SET);        N =读取(源,与放,BUF,1);        如果(N!= 1){
            fprintf中(标准错误,不能读取1个字节);
            出口(-1);
        }        N =写(DEST,&安培; BUF,1);
        如果(N!= 1){
            fprintf中(标准错误,不能写1个字节);
            出口(-1);
        }    }
    写(STDOUT_FILENO,DONE的\\ n,5);
    接近(源);
    关闭(DEST);    返回0;
}


解决方案

您只是寻求结束,并开始从那里读。难怪它不会读什么。你需要寻求到年底减1字节,读一个字节,写出来,然后寻求结束减去两个字节,读取另一个字节,依此类推。

我presume这是一个家庭作业,让你的教授不应该介意这种做法的极端低效率。 (现实世界中的性能问题是哦,所以非学历)。如果他抱怨说,告诉他,从理论上讲,它具有相同的时间复杂度与任何其他算法,将执行相同的任务:O(N)。 (这是明显的大的恩哦),他会给你一个A +。

I've got how to copy one file to another from start, but how could i modify the programme to copy it in reverse order? Source file should have read access and destination file read write execute. I have to use file control libraries.

for example

FILE A            File B should be
|---------|        |----------|
|ABCDEF   |        |FEDCBA    |
|---------|        |----------|

*********************UPDATE**********

Thank you, MikeNakis for hints and suggestions ,Sangeeth for your code

I've reworked the code and now it is copy bytes in reverse order printing filesize

here is the code

#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<sys/stat.h>
#include<unistd.h>

int main(int argc, char *argv[]) {

    int source, dest, n;
    char buf;
    int filesize;
    int i;

    if (argc != 3) {
        fprintf(stderr, "usage %s <source> <dest>", argv[0]);
        exit(-1);
    }

    if ((source = open(argv[1], 0400)) < 0) { //read permission for user on source
        fprintf(stderr, "can't open source");
        exit(-1);
    }

    if ((dest = creat(argv[2], 0700)) < 0) { //rwx permission for user on dest
        fprintf(stderr, "can't create dest");
        exit(-1);
    }

    filesize = lseek(source, (off_t) 0, SEEK_END); //filesize is lastby +offset
    printf("Source file size is %d\n", filesize);

    for (i = filesize - 1; i >= 0; i--) { //read byte by byte from end
        lseek(source, (off_t) i, SEEK_SET);

        n = read(source, &buf, 1);

        if (n != 1) {
            fprintf(stderr, "can't read 1 byte");
            exit(-1);
        }

        n = write(dest, &buf, 1);
        if (n != 1) {
            fprintf(stderr, "can't write 1 byte");
            exit(-1);
        }

    }
    write(STDOUT_FILENO, "DONE\n", 5);
    close(source);
    close(dest);



    return 0;
}

解决方案

You just seek to the end and start reading from there. No wonder it won't read anything. You need to seek to the end minus 1 byte, read one byte, write it, then seek to the end minus two bytes, read another byte, and so on.

I presume this is a homework assignment, so your professor should not mind the extreme inefficiency of this approach. (Real-world performance concerns are oh-so un-academic.) If he complains, tell him that in theory, it has the same time complexity as any other algorithm which would perform the same task: O(N). (That's pronounced "big oh of en".) He will give you an A+.

这篇关于使用lseek的相反顺序C复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆