当我尝试在 C 中复制多个文件时遇到问题。它复制目录中的第一个文件,但不再复制。其他文件甚至没有打开,我不知道问题是什么。任何的想法?
bool copyFileToDirectory(char *file, char *directory){
int input_fd, output_fd;
ssize_t ret_in, ret_out;
char* buffer[2048];
struct stat fileStat;
/* Check permissions */
access (directory, W_OK);
if (errno == EACCES) {
perror("Output file not writable");
return false;
}
if (errno == EROFS) {
perror("Output file not writable (read-only)");
return false;
}
int rval = access (file, R_OK);
if (rval != 0) {
printf ("Input file is not readable (access denied)\n");
return false;
}
/* Copy */
input_fd = open (file, O_RDONLY);
stat(file, &fileStat);
chdir(directory);
output_fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
chmod(file, fileStat.st_mode);
while((ret_in = read (input_fd, &buffer, 2048)) > 0){
ret_out = write (output_fd, &buffer, (ssize_t) ret_in);
if(ret_out != ret_in){
perror("An error has ocurred during the process\n");
return false;
}
}
close(input_fd);
close(output_fd);
return true;
}