复制文件程序中的 lseek 函数问题!
在下面的程序中使用 lseek 函数...程序只是复制文件(已经存在)。我想复制现有文件以及文件末尾的字符 例如:Sorce_File.txt 包含:复制后的“1 2 3” Target_File.txt 包含:“3 2 1”
我很确定这是简单的问题,但自从 2 天以来一直找不到如何做到这一点
#include <fcntl.h>
#include <stdio.h>
#define MAX 512
int main(int argc, char* argv[]){
char buf[MAX];
int desc_sorc, desc_targ;
int lbajt;
if (argc<3){
argv[0];
exit(1);
}
desc_sorc = open(argv[1], O_RDONLY);
if (desc_sorc == -1){
}
desc_targ = creat(argv[2], 0640);
if (desc_targ == -1){
exit(1);
}
while((lbajt = read(desc_sorc, buf, MAX)) > 0){
if (lbajt == -1) {
perror("position error");
exit(1);}
if (write(desc_targ, buf, lbajt) == -1)
{
exit(1);
}
}
if (lbajt == -1){
exit(1);
}
if (close(desc_sorc) == -1 || close(desc_targ) == -1){
exit(1);
}
exit(0);
}
Got to use lseek function in this program below... Program is simply copying file (that already exist). I wanned to copy the existing file with the chars from the end of file
for example: Sorce_File.txt contains:"1 2 3" after copy Target_File.txt contains:"3 2 1"
I'm pretty sure it's simple problem but couldn't find out since 2 days how to do it
#include <fcntl.h>
#include <stdio.h>
#define MAX 512
int main(int argc, char* argv[]){
char buf[MAX];
int desc_sorc, desc_targ;
int lbajt;
if (argc<3){
argv[0];
exit(1);
}
desc_sorc = open(argv[1], O_RDONLY);
if (desc_sorc == -1){
}
desc_targ = creat(argv[2], 0640);
if (desc_targ == -1){
exit(1);
}
while((lbajt = read(desc_sorc, buf, MAX)) > 0){
if (lbajt == -1) {
perror("position error");
exit(1);}
if (write(desc_targ, buf, lbajt) == -1)
{
exit(1);
}
}
if (lbajt == -1){
exit(1);
}
if (close(desc_sorc) == -1 || close(desc_targ) == -1){
exit(1);
}
exit(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并未将它们初始化为任何内容。任何地方。
编辑:现在你已经解决了这个问题,你真的再次测试过它吗?
You don't actually initialize these to anything. Anywhere.
EDIT: Now that you've fixed that, have you actually tested it again?
您缺少相当于
strrev(...)
的内容,用于反转您写出的字符串,以及从源文件末尾开始向后读取或从目标文件末尾写入回到最初。实际的实现留给读者作为练习。
You are missing the equivalent of
strrev(...)
in there to reverse the string you write out as well starting from the end of the source file and reading backwards or writing from the end of the target file back to the beginning.The actual implementation is left as an exercise to the reader.