C语言-IO

fopen()

C 库函数 FILE *fopen(const char *filename, const char *mode) 使用给定的模式 mode 打开 filename 所指向的文件。

FILE *fopen(const char *filename, const char *mode)

详见 传送门

fputs()

C 库函数 int fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

int fputs(const char *str, FILE *stream)  

详见 传送门

读取文本文件

char path[] = "F:\\VersionManager\\friends.txt"; // 斜杠需要变成\\转义
//打开
FILE *fp = fopen(path, "r");
if (fp == NULL)
{
    printf("文件打开失败.....");
    getchar();
    return;
}
//读取
char buff[50]; //缓存
while (fgets(buff, 50, fp))
{
    printf("%s", buff);
}
//关闭
fclose(fp);

getchar();  

写入文本文件

void method2(){
    char path[] = "F:\\VersionManager\\friends_new.txt";
    //打开
    FILE *fp = fopen(path, "w");
    char *text = "Hello World ! ";
    fputs(text, fp);

    //关闭流
    fclose(fp);
    getchar();
}

读写二进制文件

计算机的文件存储在物理上都是二进制
文本文件和二进制的之分,其实是一个逻辑之分
C读写文本文件与二进制文件的差别仅仅体现在回车换行符
写文本时,每遇到一个’\n’,就会将其转换为’\r\n’(回车换行)

文件复制

char* read_path = "F:\\VersionManager\\WorkSpace\\liuyan.png";
char* write_path = "F:\\VersionManager\\liuyan_new.png";
//读的文件,b字符表示操作二进制文件binary
FILE* read_fp = fopen(read_path, "rb");
//写文件
FILE* write_fp = fopen(write_path, "wb");
//复制
int buff[50]; //缓冲区域
int len = 0; //每次读取的数据长度
while ((len=fread(buff,sizeof(int),50,read_fp))!=0){ //一次读取50个字节,单位大小4字节(int)
    //将读到的内容写入新的文件
    fwrite(buff, sizeof(int), len, write_fp);
}
fclose(read_fp);
fclose(write_fp);
getchar();  

获取文件大小

char *read_path = "F:\\VersionManager\\liuyan.png";
FILE *fp = fopen(read_path, "r");
//重新定位文件指针
//SEEK_END文件末尾,0偏移量
fseek(fp, 0, SEEK_END); //相当于Java的ReadAccessFile
//返回当前的文件指针,相对于文件开头的位移量
long filesize = ftell(fp);
printf("%d\n", filesize);

getchar();  

实例:文件加解密

使用异或进行加解密,异或: 相同为0,不同为1

文本加密

void crpypt(char* normal_path[], char* crypt_path[]){
    //打开文件
    FILE *normal_fp = fopen(normal_path, "r");
    FILE *crypt_fp = fopen(crypt_path, "w");
    //一次读取一个字符
    int ch;
    while ((ch = fgetc(normal_fp)) != EOF) //End of File
    {
        //写入(异或运算)
        fputc(ch ^ 9, crypt_fp);
    }

    //关闭
    fclose(normal_fp);
    fclose(crypt_fp);
}  

void main(){
    char *normal_path = "F:\\VersionManager\\friends.txt";
    char *crypt_path = "F:\\VersionManager\\friends_crpypt.txt";
    //加密
    crpypt(normal_path, crypt_path);

    getchar();
}

文本解密

void decrpypt(char crypt_path[], char decrypt_path[]){
    //打开文件
    FILE *normal_fp = fopen(crypt_path, "r");
    FILE *crypt_fp = fopen(decrypt_path, "w");
    //一次读取一个字符
    int ch;
    while ((ch = fgetc(normal_fp)) != EOF){ //End of File
        //写入(异或运算)
        fputc(ch ^ 9, crypt_fp);
    }
    //关闭
    fclose(crypt_fp);
    fclose(normal_fp);
}  

void main(){
    char *crypt_path = "F:\\VersionManager\\friends_crpypt.txt";
    char *decode_path = "F:\\VersionManager\\friends_decode.txt";
    //解密
    decrpypt(crypt_path, decode_path);

    getchar();
}  

二进制文件加密

读取二进制文件中的数据时,一个一个字符读取
密码:ilovely

//打开文件
void crpypt_2(char normal_path[], char crypt_path[], char password[]){
    //打开文件
    FILE *normal_fp = fopen(normal_path, "rb");
    FILE *crypt_fp = fopen(crypt_path, "wb");
    //一次读取一个字符
    int ch;
    int i = 0; //循环使用密码中的字母进行异或运算
    int pwd_len = strlen(password); //密码的长度
    while ((ch = fgetc(normal_fp)) != EOF){ //End of File
        //写入(异或运算)
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //关闭
    fclose(crypt_fp);
    fclose(normal_fp);
}

void main(){
    char *normal_path = "F:\\VersionManager\\liuyan.png";
    char *crypt_path = "F:\\VersionManager\\liuyan_crypt.png";
    //加密
    //crpypt_2(normal_path, crypt_path,"iloveqq");

    getchar();
}

二进制文件解密

void decrpypt_2(char crypt_path[], char decrypt_path[], char password[]){
    //打开文件
    FILE *normal_fp = fopen(crypt_path, "rb");
    FILE *crypt_fp = fopen(decrypt_path, "wb");
    //一次读取一个字符
    int ch;
    int i = 0; //循环使用密码中的字母进行异或运算
    int pwd_len = strlen(password); //密码的长度
    while ((ch = fgetc(normal_fp)) != EOF){ //End of File
        //写入(异或运算)
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //关闭
    fclose(crypt_fp);
    fclose(normal_fp);
}       

void main(){
    char *crypt_path = "F:\\VersionManager\\liuyan_crypt.png";
    char *decode_path = "F:\\VersionManager\\liuyan_decode.png";
    //解密
    decrpypt_2(crypt_path, decode_path, "iloveqq");

    getchar();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值