利用文件IO的方式,实现2个文件的拷贝
int copy_by_fileIO(const char *dest_file_name, const char *src_file_name) {
int fd1 = open(dest_file_name,O_WRONLY|S_IWOTH);
if(fd1 == -1) {
perror("fd1:");
return -1;
}
int fd2 = open(src_file_name,O_RDONLY|S_IROTH);
if(fd2 == -1) {
perror("fd2:");
return -1;
}
char *buffer = (char *)malloc(2 * sizeof(char));
int ans = 0;
int k;
do{
k = read(fd2,buffer,1);
memset(buffer,0,sizeof(buffer));
write(fd1,buffer,1);
ans++;
}while(k > 0);
close(fd1);
close(fd2);
return ans;
}
利用标准IO的字符IO,实现2个文件的字节拷贝
int copy_by_char(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
char buffer = fgetc(fp2);
int cnt = 0;
while(!feof(fp2)) {
cnt++;
fputc(buffer,fp1);
buffer = fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
}
利用标准IO的行IO,实现2个文件的按行拷贝
int copy_by_line(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
char * buffer = (char *)malloc(sizeof(char) * 101);
int cnt = 0;
while(fgets(buffer,100,fp2)) {
fputs(buffer,fp1);
cnt++;
}
free(buffer);
fclose(fp1);
fclose(fp2);
return cnt;
}
利用标准IO的块IO,实现2个文件的块拷贝
int copy_by_block(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
void *buffer = (void *)malloc(2);
int cnt = 0;
while(1) {
int op = fread(buffer,1,1,fp2);
if(op <= 0) break;
fwrite(buffer,1,1,fp1);
cnt++;
}
free(buffer);
fclose(fp1);
fclose(fp2);
return cnt;
}
汇总
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int copy_by_fileIO(const char *dest_file_name, const char *src_file_name) {
int fd1 = open(dest_file_name,O_WRONLY|S_IWOTH);
if(fd1 == -1) {
perror("fd1:");
return -1;
}
int fd2 = open(src_file_name,O_RDONLY|S_IROTH);
if(fd2 == -1) {
perror("fd2:");
return -1;
}
char *buffer = (char *)malloc(2 * sizeof(char));
int ans = 0;
int k;
do{
k = read(fd2,buffer,1);
memset(buffer,0,sizeof(buffer));
write(fd1,buffer,1);
ans++;
}while(k > 0);
close(fd1);
close(fd2);
return ans;
}
int copy_by_char(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
char buffer = fgetc(fp2);
int cnt = 0;
while(!feof(fp2)) {
cnt++;
fputc(buffer,fp1);
buffer = fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
}
int copy_by_line(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
char * buffer = (char *)malloc(sizeof(char) * 101);
int cnt = 0;
while(fgets(buffer,100,fp2)) {
fputs(buffer,fp1);
cnt++;
}
free(buffer);
fclose(fp1);
fclose(fp2);
return cnt;
}
int copy_by_block(const char *dest_file_name, const char *src_file_name) {
FILE *fp1 = fopen(dest_file_name,"w");
FILE *fp2 = fopen(src_file_name,"r");
if(fp1 == NULL) {
perror("fp1:");
return -1;
}
if(fp2 == NULL) {
perror("fp2:");
return -1;
}
void *buffer = (void *)malloc(2);
int cnt = 0;
while(1) {
int op = fread(buffer,1,1,fp2);
if(op <= 0) break;
fwrite(buffer,1,1,fp1);
cnt++;
}
free(buffer);
fclose(fp1);
fclose(fp2);
return cnt;
}
int main() {
char *src_file_path = "/etc/profile";
char *dest_file_path = "/tmp/test_profile";
int cnt = copy_by_fileIO(dest_file_path,src_file_path);
printf("cnt = %d\n",cnt);
return 0;
}
|