c 使用 lseek 以相反顺序复制文件
我已经知道如何从一开始就将一个文件复制到另一个文件,但是我如何修改程序以按相反的顺序复制它?源文件应具有读取访问权限,目标文件应具有读写执行权限。我必须使用文件控制库。
例如
FILE A File B should be
|---------| |----------|
|ABCDEF | |FEDCBA |
|---------| |----------|
***********< em>*******< /em>***更新**********
谢谢 MikeNakis 的提示和建议 ,Sangeeth 对于你的代码
我已经重新编写了代码,现在它是以相反的顺序复制字节打印文件大小
这里是代码
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需找到最后并从那里开始阅读即可。难怪它不会读取任何内容。您需要查找到末尾减去 1 个字节,读取一个字节,写入它,然后查找到末尾减去两个字节,读取另一个字节,依此类推。
我认为这是一项家庭作业,所以你的教授不应该介意这种方法的效率极低。 (现实世界中的性能问题非常不学术。)如果他抱怨,请告诉他,理论上,它与执行相同任务的任何其他算法具有相同的时间复杂度:O(N)。 (发音为“big oh of en”。)他会给你 A+。
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(源, (off_t) i, SEEK_SET);可能应该是 lseek(source, (off_t) i - 1, SEEK_SET);
lseek(source, (off_t) i, SEEK_SET); should probably be lseek(source, (off_t) i - 1, SEEK_SET);