C的io操作

2017-11-28  本文已影响247人  耳_总

字符读写:

int main(int argc, const char * argv[]) {
    char *path = "/Users/xucong/workspace/cio.txt";
    FILE *fp = fopen(path, "r");
    if(fp == NULL) {
        printf("打开文件失败");
        return 0;
    }
    char buffer[50];
    while (fgets(buffer, 50, fp)) {
        printf("ccccc");
        printf("%s",buffer);
    }
    fclose(fp);
    
    return 0;
}

打开文件:fopen(path,mode),第一个参数为文件的路径,第二个参数为读写的模式r:读,w:写,rb:读取二进制流,wb:读取二进制流。
读文件函数:
fgets():读取字符串;
fgetc():读取字符
fread():读取二进制流
fputs():写入字符串
fputc():写入字符
fwrite():写入流
这个几函数命名也能看出来,f代表文件,中间代表功能,最后s-字符串,c-字符。
流读写:

void main() {
    char *read_path = "/Users/xucong/workspace/cio.txt";
    char *write_path = "/Users/xucong/workspace/cio_new.txt";
    
    FILE *rfp = fopen(read_path, "rb");
    FILE *wfp = fopen(write_path, "wb");
    
    int buffer[10];
    int len = 0;
    while ((len = fread(buffer, sizeof(int), 10, rfp)) != 0) {
        fwrite(buffer, sizeof(int), len, wfp);
    }
    fclose(rfp);
    fclose(wfp);
    getchar();
}

说下(len = fread(buffer, sizeof(int), 10, rfp)) != 0函数,fread()函数第一个函数为void* 任意类型的指针,为缓冲区buffer数组,第二个参数表示第一个参数类型的大小,第三个参数表示每次读写多少个,比如:int buffer,那么 每次读的大小为sizeof(int) * 第三个参数,最后一个参数为文件指针,len为每次实际读取的大小。write函数类似。

void main() {
    char *path = "/Users/xucong/workspace/cio.txt";
    FILE *fp = fopen(path, "r");
    //最后一个参数表示将文件指针移动到文件末尾,第二个参数表示
    //相对于第三个参数的偏移量
    fseek(fp, 0, SEEK_END);
    //ftell返回文件相对于文件初始位置的偏移量,
    long size = ftell(fp);
    printf("%ld",size);
    getchar();
}

练习:

/*
//异或
//规则:1^1=0, 0^0=0, 1^0=1, 0^1=1 同为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(crypt_fp);
    fclose(normal_fp);
}

//解密
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 *normal_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends.txt";
    char *crypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends_crypt.txt";
    char *decrypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends_decrypt.txt";

    //crpypt(normal_path, crypt_path);
    //解密
    decrpypt(crypt_path, decrypt_path);

    getchar();
}

一般android里面的一些比较隐私的文件需要加解密操作,这时候需要放在so库里面编写,这样不容易内破解,一般加密算法是用抑或算法。
上面的代码还可以改进,或者用流的形式加密也可以的,只是把r、w改为rb、wb。

void crpypt(char read_path[],char write_path[],char password[]) {
    FILE *rfp = fopen(read_path, "rb");
    FILE *wfp = fopen(write_path, "wb");
    
    int buffer;
    long i = 0;
    unsigned long lenth = strlen(password);
    while ((buffer = fgetc(rfp)) != EOF) {
        fputc(buffer ^ password[i = i%lenth], wfp);
        i++;
    }
    
    fclose(rfp);
    fclose(wfp);
    
}

void main() {
    char *normoal_path = "/Users/xucong/许聪/动脑资料/NDK/08_08_C_05/files/friends.txt";
    char *crpypt_path = "/Users/xucong/许聪/动脑资料/NDK/08_08_C_05/files/friends_crpypt.txt";
    char *decrpypt_path = "/Users/xucong/许聪/动脑资料/NDK/08_08_C_05/files/friends_decrpypt.txt";
    
    crpypt(normoal_path,crpypt_path,"love");
    crpypt(crpypt_path,decrpypt_path,"love");
}
上一篇 下一篇

猜你喜欢

热点阅读