C语言IO操作

#include

#include

#include

//读文件

/*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;

}