复制文件程序中的 lseek 函数问题!

发布于 2024-08-13 03:26:36 字数 858 浏览 2 评论 0原文

在下面的程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-08-20 03:26:36
 int desc_sorc, desc_targ;

您实际上并未将它们初始化为任何内容。任何地方。

编辑:现在你已经解决了这个问题,你真的再次测试过它吗?

 int desc_sorc, desc_targ;

You don't actually initialize these to anything. Anywhere.

EDIT: Now that you've fixed that, have you actually tested it again?

╭⌒浅淡时光〆 2024-08-20 03:26:36

您缺少相当于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文