C语言IO操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//读文件
/*int main(){
    char *path = "/Users/linyaokui/Downloads/a.txt";
    FILE *file = fopen(path, "r");
    char buffer[5];
    while (fgets(buffer, 5, file)) {
        printf("%s",buffer);
    }
    fclose(file);
    return 0;
}*/
//写文件
/*int main(){
    char *text = "测试";
    char *path = "/Users/linyaokui/Downloads/ab.txt";
    FILE *file = fopen(path, "a");
    if(file != NULL){
        fputs(text, file);
        fclose(file);
    }
    return 0;
}*/
//写二进制文件
/*
int main(){
    char *readPath = "/Users/linyaokui/Downloads/timg.jpeg";
    char *writePath = "/Users/linyaokui/Downloads/timg_write.jpeg";
    FILE *fileRead = fopen(readPath, "rb");
    FILE *fileWrite = fopen(writePath,"wb");
    char buffer[50];
    long len = 0;
    while ((len = fread(buffer, sizeof(char), 50, fileRead)) != 0) {
        fwrite(buffer, sizeof(char), len, fileWrite);
    }
    fclose(fileRead);
    fclose(fileWrite);
    return 0;
}
 */
//读取文件大小
/*
int main(){
    char *path = "/Users/linyaokui/Downloads/timg.jpeg";
    FILE *file = fopen(path, "r");
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    fclose(file);
    printf("%ld",size);
    return 0;
}
*/
/*
void encode(char *normalFilePath,char *encodeFilePath){
    FILE *nFile = fopen(normalFilePath, "r");
    FILE *eFile = fopen(encodeFilePath, "w");
    int ch;
    while ((ch = fgetc(nFile)) != EOF) {
        fputc(ch^9, eFile);
    }
    fclose(nFile);
    fclose(eFile);
}
void decode(char *ePath,char *dPath){
    FILE *eFile = fopen(ePath, "r");
    FILE *dFile = fopen(dPath, "w");
    int ch;
    while ((ch = fgetc(eFile)) != EOF) {
        fputc(ch^9, dFile);
    }
    fclose(eFile);
    fclose(dFile);  
}
//文本文件加解密
int main(){
    char *nPath = "/Users/linyaokui/Downloads/a.txt";
    char *ePath = "/Users/linyaokui/Downloads/an.txt";
    char *dPath = "/Users/linyaokui/Downloads/ad.txt";
//    encode(nPath, ePath);
    decode(ePath, dPath);
    return 0;
}
*/
//二进制文件加密
/*
void encode_b(char *normalFilePath,char *encodeFilePath ,char *password){
    FILE *nFile = fopen(normalFilePath, "rb");
    FILE *eFile = fopen(encodeFilePath, "wb");
    int ch;
    int i = 0;
    int pd_lenght = (int)strlen(password);
    while ((ch = fgetc(nFile)) != EOF) {
        fputc(ch^password[i%pd_lenght], eFile);
        i++;
        i = i%1024;
    }
    fclose(nFile);
    fclose(eFile);
}
void decode_b(char *encodeFilePath,char *dcodeFilePath ,char *password){
    FILE *eFile = fopen(encodeFilePath, "rb");
    FILE *dFile = fopen(dcodeFilePath, "wb");
    int ch;
    int i = 0;
    int pd_lenght = (int)strlen(password);
    while ((ch = fgetc(eFile)) != EOF) {
        fputc(ch^password[i%pd_lenght], dFile);
        i++;
        i = i%1024;
    }
    fclose(eFile);
    fclose(dFile);
}
int main(){
    char *password = "iphone";
    char *nPath = "/Users/linyaokui/Downloads/timg.jpeg";
    char *ePath = "/Users/linyaokui/Downloads/e.jpeg";
    char *dPath = "/Users/linyaokui/Downloads/d.jpeg";   
//    encode_b(nPath, ePath,password);
    decode_b(ePath,dPath,password);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值