开发者

lseek function problem in a copy file program!

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, b开发者_运维技巧uf, lbajt) == -1)

{
   exit(1);


    }
  } 
 if (lbajt == -1){
    exit(1);
 } 

 if (close(desc_sorc) == -1 || close(desc_targ) == -1){
    exit(1);
 }

 exit(0);
 }


 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜