#include <stdio.h>
int main() {
FILE *source, *target;
char buffer[1024];
size_t bytes;
// 使用C库函数备份文件
source = fopen("source.dat", "rb");
if (source == NULL) {
perror("Error opening source file");
return 1;
}
target = fopen("target.dat", "wb");
if (target == NULL) {
perror("Error opening target file");
fclose(source);
return 1;
}
while ((bytes = fread(buffer, 1, sizeof(buffer), source)) > 0) {
fwrite(buffer, 1, bytes, target);
}
fclose(source);
fclose(target);
printf("File backup using C library functions completed successfully\n");
return 0;
}
22222222222222222222
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main() {
FILE *source, *target;
char buffer[1024];
size_t bytes;
source = fopen("source.dat", "rb");
if (source == NULL) {
perror("Error opening source file");
return 1;
}
target = fopen("target.dat", "wb");
if (target == NULL) {
perror("Error opening target file");
fclose(source);
return 1;
}
while ((bytes = fread(buffer, 1, sizeof(buffer), source)) > 0) {
fwrite(buffer, 1, bytes, target);
}
fclose(source);
fclose(target);
printf("File backup using C library functions completed successfully\n");
// 设置目标文件权限为只读
if (chmod("target.dat", S_IREAD) != 0) {
perror("Error setting file permissions");
return 1;
}
return 0;
}