1

私の目的は、バイナリ ファイル内の 1 つ以上の部分を削除することです。これを行うには、必要な部分のみを 2 番目のファイルにコピーします。私は2つの方法を持っています。最初のものはFile1 (オフセットskip付き) から File2 にcountバイトを追加する必要があります。

void copyAPart(struct handle* h, off_t skip, off_t count) {

 struct charbuf *fileIn = NULL;
 struct charbuf *fileOut = NULL;
 fileIn = charbuf_create();
 fileOut = charbuf_create();
 int fin, fout, x, i;
 char data[SIZE];

 charbuf_putf(fileIn,"%s/File1", h->directory);
 charbuf_putf(fileOut,"%s/File2", h->directory);

 fin = open(charbuf_as_string(fileIn), O_RDONLY);
 fout = open(charbuf_as_string(fileOut), O_WRONLY|O_CREAT, 0666);

 lseek(fin, skip, SEEK_SET);
 lseek(fout,0, SEEK_END);

 while(i < count){
   if(i + SIZE > count){
       x = read(fin, data, count-i);
    }else{
       x = read(fin, data, SIZE);
    }
    write(fout, data, x);
    i += x;
    }

 close(fout);
 close(fin);
 charbuf_destroy(&fileIn);
 charbuf_destroy(&fileOut);
}

2 番目のメソッドは、File1 の残り (スキップから最後まで) を File2 に追加する必要があります。

void copyUntilEnd(struct handle* h, off_t skip) {

 struct charbuf *fileIn = NULL;
 struct charbuf *fileOut = NULL;
 fileIn = charbuf_create();
 fileOut = charbuf_create();
 int fin, fout, x, i;
 char data[SIZE];

 charbuf_putf(fileIn,"%s/File1", h->directory);
 charbuf_putf(fileOut,"%s/File2", h->directory);

 fin = open(charbuf_as_string(fileIn), O_RDONLY);
 fout = open(charbuf_as_string(fileOut), O_WRONLY|O_CREAT, 0666);

 lseek(fin, skip, SEEK_SET);
 lseek(fout,0, SEEK_END);
 x = read(fin, data, SIZE);

 while(x>0){
    write(fout, data, x);
    x = read(fin, data, SIZE);
  }

 close(fout);
 close(fin);
 charbuf_destroy(&fileIn);
 charbuf_destroy(&fileOut);
}

私の質問は次のとおりです。

  1. これが期待どおりに機能しないのはなぜですか?
  2. これを 64 ビット システムの大きなファイル (>4GB) で使用するには、何を変更する必要がありますか?

前もって感謝します

4

1 に答える 1