【C语言】文件的读写、复制

1 篇文章 0 订阅

C语言文件的读写与加密、解密


前言

本文主要记录在C语言开发过程中关于文件操作与实现的内容。


一、文件的读

对文件进行读取,是我们在C开发过程中可能遇到的。下面是以Windows平台为例,介绍的一种常见的实现方式;
实现前操作,导入头文件;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

文件的操作在头文件stdlib.h里面,因而,使用前需要进行导入这个文件。然后定义一个main函数;使用fopen()打开文件;首先让我们来看一下这个函数;FILE * fopen (const char *, const char *);
这个函数一共有两个参数,第一个参数一般用于存放文件路径;第二个参数存放操作模式:r、w、rb、rw等;其中r代表读;w代表写文件;rb作为二进制文件的读;rw作为二进制文件的写;这个函数最终返回FILE结构体
在这里插入图片描述


int main(){
char * fileName = "D:\\a.txt";
    FILE * file = fopen(fileName, "r");
    if (!file) {
        printf("文件打开失败");
        exit(0); // 退出程序
    }
    // 先定义缓存区域 (容器)
    char buffer[100];
    // 1.缓冲区buffer, 2:长度100, 3:文件指针变量
    while (fgets(buffer, 10, file)) {
        printf("%s", buffer);
    }
    // 关闭文件
    fclose(file);

return 0;
}



二、文件的写

1.导入头文件

文件操作一般在头文件stdlib.h;

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.打开文件路径

调用FILE * fopen (const char *, const char *); 打开文件的路径;注意:当第二个参数使用‘w’时,在打开文件时,若文件不存在,会自动创建一共为0KB的空文件;

FILE * file = fopen(fileNameStr, "w");

3.写入文件

使用 fputs()往文件中写入数据


    fputs("123456", file);

4.完整实现

int main() {
    char * fileName = "D:\\a.txt";
    FILE * file = fopen(fileName, "w");
    if (!file) {
        printf("文件打开失败");
        exit(0); // 退出程序
    }

    fputs("123456...", file);
    // 关闭文件
    fclose(file);
    return 0;
}

三、文件的复制

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.打开文件

调用FILE * fopen (const char *, const char *); 打开文件的路径;

3.读取文件并写入

读取二进制数据

FILE * file = fopen(fileName, "rb");

写入二进制数据

FILE * fileCopy = fopen(fileNameStr, "rw");

4.复制的完整实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {

    
    char * fileName = "D:\\a.txt"; // 来源
    char * fileNameCopy = "D:\\b.txt"; // 目标
    FILE * file = fopen(fileName, "rb");
    FILE * fileCopy = fopen(fileNameCopy, "wb");
    if (!file || !fileCopy) {
        printf("文件打开失败");
        exit(0); // 退出程序
    }
    int buffer[100]; // 100 * 4 = 400
    int len; // 每次读取的长度
    // fread:参数1:容器buffer, 参数2:每次偏移多少 int, 参数3:容器大小 写个400,等下文件就报废了
    // sizeof(buffer) / sizeof(int) 等价与 100
    while ((len = fread(buffer, sizeof(int), 100 , file)) != 0) {
        fwrite(buffer, sizeof(int), len, fileCopy);
    }

    // 关闭文件
    fclose(file);
    fclose(fileCopy);
    return 0;
}

四、获取文件的大小

C没有专门的API用来获取文件大小的API;可以根据指针从第0为到最后一位挪动的位置判断文件的大小;

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.代码实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    char * fileName = "D:\\a.txt"; 
    FILE * file = fopen(fileName, "rb"); 
    if (!file) {
        printf("文件打开失败");
        exit(0); // 退出程序
    }
    // SEEK_SET(开头)  SEEK_CUR(当前)  SEEK_END(结尾)
    fseek(file, 0, SEEK_END);
    // 给file指针赋值,挪动的记录信息
    // 读取  file赋值的记录信息
    // 其实此函数目的是:计算偏移的位置,ftell 从 0 开始统计到当前(SEEK_END)
    long file_size = ftell(file);
    printf("%s文件的字节大小是:%ld\n", fileName, file_size);
    // 8 字节 (8 字节)
    // 关闭文件
    fclose(file);
    return 0;
}


五、文件的加密

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.代码实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    char * fileName = "D:\\a.jpg"; // 加密前的文件
    char * fileNameEncode = "D:\\b.jpg"; // 加密后的目标文件

    FILE * file = fopen(fileName, "rb"); // 打开需要加密的文件
    FILE * fileEncode = fopen(fileNameEncode, "wb"); //创建目标文件

    if (!file || !fileEncode) {
        printf("文件打开失败");
        exit(0); 
    }
    //
    int c; // 接收读取的值
    while ((c = fgetc(file)) != EOF) {
        // 加密操作 这里采用的是异或的方式进行加密;解密的时候也需要通过异或的方式进行解密
        // 写入到 fileEncode  D:\b.jpg(加密后的图片)
        fputc(c ^ 5, fileEncode ); 
    }
    fclose(file);
    fclose(fileEncode);

    return 0;
}

六、文件的解密

通过异或的方式进行解密

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.代码实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    char *fileName = "D:\\a.jpg"; //解密前的文件
    char *fileNameStrDecode = "D:\\b.jpg"; // 解密后的文件

    FILE * file = fopen(fileNameStr, "rb"); 
    
    FILE * fileEncode = fopen(fileNameStrDecode, "wb"); 
 

    if (!file || !fileEncode) {
        printf("文件打开失败");
        exit(0); // 退出程序
    }

    int c;
    while ((c = fgetc(file)) != EOF) {
        // 通过异或的方式进行还原
        fputc(c ^ 5, fileEncode);
    }

    fclose(file);
    fclose(fileEncode);

    return 0;
}

七、文件使用密钥的方式进行加密

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.代码实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    char *fileName = "D:\\a.jpg"; // 加密前的文件
    char * fileNameStrEncode = "D:\\b.jpg"; // 加密后的文件

    // 密钥
    char * password = "123456"; 

    FILE * file = fopen(fileNameStr, "rb");
    FILE * fileEncode = fopen(fileNameStrEncode, "wb"); 

    if (!file || !fileEncode) {
        printf("文件打开失败");
        exit(0); 
    }

    int c;
    int index = 0;
    int pass_len = strlen(password);
    while ((c = fgetc(file)) != EOF) {
        char item = password[index%pass_len];
        // 1 2 3 4 5 6  1 2 3 4 5 6  1 2 3 4 5 6 ...
        printf("item:%c%\n", item);
        fputc(c ^ item, fileEncode);
        index ++;
    }

    // 关闭文件
    fclose(file);
    fclose(fileEncode);
}

八、文件使用密钥的方式进行解密

1.导入头文件

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

2.代码实现

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    char *fileName = "D:\\a.jpg"; // 加密前的文件
    char * fileNameStrEncode = "D:\\b.jpg"; // 加密后的文件

    FILE * file = fopen(fileNameStr, "rb");
    FILE * fileEncode = fopen(fileNameStrEncode, "wb"); 
    if (!file || !fileEncode) {
        printf("文件打开失败");
        exit(0); 
    }
	//密钥
	char * password = "123456";
  	 int  c;
    int index = 0;
    int pass_len = strlen(password);
    while ((c = fgetc(file)) != EOF) {
        fputc(c ^ password[index%pass_len], fileDecode);
        index++;
    }

    fclose(file);
    fclose(fileDecode);
return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值