1
我的目標是刪除二進制文件中的一個或多個部分。我只需將需要的部分複製到第二個文件即可。我有兩種方法。第一個應該附加計數字節從文件1(與偏移量跳過)到File2。C用lseek /讀/寫刪除一個大的二進制文件的部分
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);
}
那麼第二種方法應該附加文件1的其餘部分(從跳到終點)到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);
}
我的問題如下:
- 這是爲什麼不工作的預期?
- 需要更改哪些內容才能在64位系統上的大文件(> 4GB)上使用它?
預先感謝
您可以嘗試初始化我 – Dipstick 2014-09-25 11:52:03
沒有改變該文件2只是原始文件1的副本,並且該程序被卡在某處的循環:( – reencode 2014-09-25 12:20:01