-1
我試圖做一個函數打開2個文件 - 一個用於讀取,另一個用於寫入,然後將第一個文件複製到另一個二進制文件中,由於某種原因它只是不起作用。我試圖調試該程序幾次,但我無法識別該問題。將img文件的內容複製到另一個img文件
void myCopyBinary(char * * argv) {
FILE * srcFile;
FILE * dstFile;
int yesNo = 0;
char temp = ' ';
int i = 0;
int size = 0;
char * buffer;
int resultFread = 0;
int resultFwrite = 0;
srcFile = fopen(argv[1], "rb");
if (srcFile != NULL) {
dstFile = fopen(argv[2], "rb");
if (dstFile != NULL) {
printf("Would you like to overwrite it? (Every Number -YES, 0-NO): ");
scanf("%d", & yesNo);
if (yesNo == 0) {
fclose(dstFile);
exit(0);
return 1;
}
}
dstFile = fopen(argv[2], "wb");
if (dstFile != NULL) {
fseek(srcFile, 0, SEEK_END); // non-portable
size = ftell(srcFile);
buffer = (char *) malloc(sizeof(char) * size);
if (buffer == NULL) {
printf("Error with the buffer!\n");
exit(1);
}
do {
resultFread = fread(buffer, 1, sizeof(buffer), srcFile);
resultFwrite = fwrite(buffer, 1, resultFread, dstFile);
} while (resultFread > 0);
}
}
爲什麼不使用'cp file1 file2'? –
如果是800GB文件會怎麼樣?嘗試將所有內容讀入內存可能不是一個好主意...... –
你是怎麼調用這個例程的? – usr2564301