文件复制的C语言实现

博主在尝试使用C语言实现文件复制的过程中遇到了一系列问题,包括:目标文件存在判断错误、文件拷贝后末尾出现乱码、处理绝对路径时的文件名获取以及变量改动后仍影响文件写入等。通过分析和调试,博主记录了这些挑战及其解决思路。
摘要由CSDN通过智能技术生成

最近在看国嵌的嵌入式视频,必修实验中的一个作业引起了我的兴趣:用库函数实现文件的复制


所以这几天都在写程序,虽然遇到不少问题,不过也算是学习的一点经历,因此写博来总结一下。


先贴实现的思路跟程序



<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)
	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值