最近在看国嵌的嵌入式视频,必修实验中的一个作业引起了我的兴趣:用库函数实现文件的复制
所以这几天都在写程序,虽然遇到不少问题,不过也算是学习的一点经历,因此写博来总结一下。
先贴实现的思路跟程序
<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc,char *argv[])
{
//define variables
FILE *sfile = NULL,*dfile = NULL;
struct stat sfile_info,dfile_info;
char *ptr = NULL;
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE),*sfile_name = NULL;
int data_read = 0,data_write = 0,file_len = 0,tmp_len = 0,path_len = 0,pos = 0;
int i = 0;
unsigned int file_size_byte = 0,file_size_kb = 0,file_size_mb =0,file_size_gb = 0;
char order ;
//initializion and judgement
bzero(buffer,sizeof(buffer));
if(ptr != NULL && sfile != NULL && dfile != NULL && sfile_name !=NULL)
exit(1);
//the judgement of parameter number
if(argc == 1 )
{
printf("\nHey,Hey,boy,you don't tell me the names of the source file and the target file\n ");
exit(1);
}
if(argc == 2)
{
printf("\n..........any more? The target file name TAT\n");
exit(1);
}
//if the source file existed ?
if(access(argv[1],F_OK)==-1)
{
printf("\nAre you kidding me? The source file do not exist!\n");
exit(1);
}
//a file or a a directory
stat(argv[1],&sfile_info);
if(S_ISDIR(sfile_info.st_mode))
{
printf("\n.......The first path is pointing to adirectory,give me the path of the source file...\n");
exit(1);
}
if(access(argv[1],R_OK)==-1)
{
printf("\nEh,the source file can not be read....\n");
exit(1);
}
//deal with the target file
while(dfile == NULL)