#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 4096
void copy_file(const char *source_file, const char *dest_file) {
FILE *source, *dest;
char buffer[BUFFER_SIZE];
size_t bytesRead;
// 打开源文件以读取
source = fopen(source_file, "rb");
if (source == NULL) {
perror("无法打开源文件");
exit(EXIT_FAILURE);
}
// 打开目标文件以写入
dest = fopen(dest_file, "wb");
if (dest == NULL) {
perror("无法创建目标文件");
fclose(source);
exit(EXIT_FAILURE);
}
// 从源文件读取内容并写入目标文件
while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) {
fwrite(buffer, 1, bytesRead, dest);
}
// 关闭文件
fclose(source);
fclose(dest);
printf("文件复制成功: %s -> %s\n", source_file, dest_file);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "用法: %s <源文件名> <目标文件名>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *source_file = argv[1];
const char *dest_file = argv[2];
copy_file(source_file, dest_file);
return 0;
}
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
void get_file_permissions(const char *filename) {
struct stat file_stat;
// 使用stat函数获取文件属性
if (stat(filename, &file_stat) == -1) {
perror("获取文件属性失败");
return;
}
// 用户权限
printf("用户权限: ");
printf((file_stat.st_mode & S_IRUSR) ? "r" : "-");
printf((file_stat.st_mode & S_IWUSR) ? "w" : "-");
printf((file_stat.st_mode & S_IXUSR) ? "x" : "-");
printf("\n");
// 组权限
printf("组权限: ");
printf((file_stat.st_mode & S_IRGRP) ? "r" : "-");
printf((file_stat.st_mode & S_IWGRP) ? "w" : "-");
printf((file_stat.st_mode & S_IXGRP) ? "x" : "-");
printf("\n");
// 其他用户权限
printf("其他用户权限: ");
printf((file_stat.st_mode & S_IROTH) ? "r" : "-");
printf((file_stat.st_mode & S_IWOTH) ? "w" : "-");
printf((file_stat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n");
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "用法: %s <文件名>\n", argv[0]);
return 1;
}
const char *filename = argv[1];
// 获取文件权限并输出结果
get_file_permissions(filename);
return 0;
}