zoukankan      html  css  js  c++  java
  • C语言实现文件复制

    #include <stdio.h>
    #include <stdlib.h>
    
    int copyFile(const char* dest, const char* src)
    {
    	FILE* fin = fopen(dest, "r");
    	FILE* fout = fopen(src, "w");
    
    	if (fin && fout)
    	{
    		while (!feof(fin))//读到最后一个字符,feof(fin)仍未false
    		{
    			fputc(fgetc(fin), fout);
    		}
    		fclose(fin);
    		fclose(fout);
    		return 0;
    	}
    	return -1;
    }
    
    int main(int argc, char* argv[])
    {
    	if (argc > 2)
    	{
    		if (copyFile(argv[1], argv[2]) != 0)
    			printf("文件复制失败\n");
    		else
    		{
    			long long len;
    			FILE *fp;
    			fp = fopen(argv[1],"r");
    			fseek(fp,0L,SEEK_END);
    			len = ftell(fp);
    			rewind(fp);
    			printf("输入文件大小:%lld 字节\n",len);
    			printf("文件内容:\n");
    			while (!feof(fp))
    			{
    				putchar(fgetc(fp));
    			}
    			printf("\n");
    			fclose(fp);
    
    
    
    			fp = fopen(argv[2],"r");
    			fseek(fp,0L,SEEK_END);
    			len = ftell(fp);
    			rewind(fp);
    			printf("输出文件大小:%lld 字节\n",len);
    			printf("文件内容:\n");
    			while (!feof(fp))
    			{
    				putchar(fgetc(fp));
    			}
    			printf("\n");
    			fclose(fp);
    		}
    	}
    }
    

    命令行:

    copy.exe in.txt out.txt

    注意out.txt读入了in.txt的文件结束标志EOF,使得两个文件的大小不一样!






  • 相关阅读:
    uniGUI之uniEdit(23)
    ​Shiro授权
    Shiro密码重试次数限制
    Ehcache基础入门
    Shiro简单加密服务
    Shiro身份验证
    第二章、Web中使用shiro(实现登陆)
    第一章、认识Shiro
    使用IntelliJ/Eclipse生成类图
    Jedis操作Redis实例
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835245.html
Copyright © 2011-2022 走看看