美文网首页
C++之文件

C++之文件

作者: JasonChen8888 | 来源:发表于2020-05-18 17:12 被阅读0次

    C++ 文件处理

    官方文档:http://www.cplusplus.com/reference/cstdio/fopen/
    相关内容简介

    • fopen方法的介绍
    • 文本文件读取
    • 写入文本文件
    • 二进制文件读写
    • 读取文件大小
    • 文本文件加解密
    • 二进制文件的加解密

    fopen

    fopen( ) 函数来创建一个新的文件或者打开一个已有的文件,这个调用会初始化类型 FILE 的一个对象,类型 FILE 包含了所有用来控制流的必要的信息

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

    filename:文件的路径
    mode:表示打开文件的模式,如下图:


    mode类别.png

    二进制文件和文本文件读写的区别:
    写文本 '\n’-> \r\n
    读文本 \r\n -> \n

    文本文件读取

    test.txt:

    这只是一个单纯的文件
    用来测试的,不要想多了。
    

    栗子代码:

    int main()
    {
        char *path = "D:\\NDK\\class\\files\\test.txt";  //必修使用两个\\ 否则会报错
        
        FILE *fp = fopen(path, "r");
            
        if (fp == NULL) {
             printf("文件不存在,或者加载文件失败...");
             return 0;
        }
    
        char buff[500];
        while (fgets(buff, 50, fp))
        {
             printf("%s", buff);
         }
    
        fclose(fp);
        system("pause");
        return 0;
    }
    

    写入文本文件

    test.txt 还是上面的文件
    栗子代码:

    int main() {
    
        char *path = "D:\\NDK\\class\\files\\test.txt";
        FILE *fp = fopen(path, "w");
    
        if (fp == NULL) {
            printf("failed。。。。");
            return 0;
        }
        
        char *text = "C++开发,我看好你";
        fputs(text, fp);
        fclose(fp);
    
    
        system("pause");
        return 0;
    }
    

    二进制文件读写

    实现一个exe文件读取,再进行输出到新的路径
    用到了fread和fwrite两个读和写的方法

    size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
    

    从给定流 stream 读取数据到 ptr 所指向的数组中

    size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
    

    把 ptr 所指向的数组中的数据写入到给定流 stream 中

    栗子代码:

    int main() {
        char * read_path = "D:\\NDK\\class\\files\\testPro.exe";
        char * write_path = "D:\\NDK\\class\\files\\testPro_write.exe";
    
        //read 
        FILE * read_fp = fopen(read_path, "rb");   //rb 表示读取二进制
        //write
        FILE * write_fp = fopen(write_path, "wb");  //wb 表示写入二进制
        char buff[50];
        int len = 0;
        while ((len = fread(buff, sizeof(char), 50, read_fp)) != 0) 
        {
            fwrite(buff, sizeof(char), len, write_fp);
        }
        fclose(read_fp);
        fclose(write_fp);
        system("pause");
        return 0;
    }
    

    读取文件大小

    先看两个方法:

    1. fseek :设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。
    int fseek(FILE *stream, long int offset, int whence)
    

    stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
    offset -- 这是相对 whence 的偏移量,以字节为单位。
    whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:


    whence.png
    1. ftell:返回给定流 stream 的当前文件位置。
    long int ftell(FILE *stream)
    

    栗子代码:读取图片文件的大小

    int main() {
        char *read_path = "D:\\NDK\\class\\files\\beauty.png";
    
        FILE *fp = fopen(read_path, "r");
        if (fp == NULL)
        {
            return 0;
        }
    
        fseek(fp, 0, SEEK_END);  //表示文件指向文件末尾
        long  filesize = ftell(fp);  //获取当前文件位置
    
        printf("%ld \n", filesize);
    
        system("pause");
        return 0;
    }
    

    文本文件加解密

    异或算法:不相同的为1,相同为0
    1001 ^ 0111 = 1110
    1110 ^ 0111 = 1001

    加密上面的test.txt文件,代码:

    void encode(char normal_path[], char encode_path[]) {
        FILE * normal_fp = fopen(normal_path, "r");
        FILE * encode_fp = fopen(encode_path, "w");
    
        int ch;
        while ((ch = fgetc(normal_fp)) != EOF){
            fputc(ch ^ 7, encode_fp);
        }
        fclose(normal_fp);
        fclose(encode_fp);
    }
    
    void decode(char encode_path[], char decode_path[]) {
        FILE * normal_fp = fopen(encode_path, "r");
        FILE * encode_fp = fopen(decode_path, "w");
    
        int ch;
        while ((ch = fgetc(normal_fp)) != EOF) {
            fputc(ch ^ 7, encode_fp);    
        }
        fclose(normal_fp);
        fclose(encode_fp);
    }
    
    int main() {
        char * nomal_path = "D:\\NDK\\class\\files\\test.txt";
        char * encode_path = "D:\\NDK\\class\\files\\test_encode.txt";
        char * decode_path = "D:\\NDK\\class\\files\\test_decode.txt";
        //encode(nomal_path, encode_path);
        decode(encode_path, decode_path);
        system("pause");
        return 0;
    }
    

    二进制文件的加解密

    这边采用的传进加密密码,加密和解密的密码必须一致,否则,无法解密出来

    void biEncode(char normal_path[], char encode_path[], char * password) {
        FILE * normal_fp = fopen(normal_path, "rb");
        FILE * encode_fp = fopen(encode_path, "wb");
    
        int ch;
        int pwd_legth = strlen(password);
        int i = 0;
        while ((ch = fgetc(normal_fp)) != EOF){
            fputc(ch ^ password[i % pwd_legth], encode_fp);
            i = (i++) % 10001;
        }
        fclose(normal_fp);
        fclose(encode_fp);
    }
    
    void biDecode(char encode_path[], char decode_path[], char * password) {
        FILE * normal_fp = fopen(encode_path, "rb");
        FILE * encode_fp = fopen(decode_path, "wb");
    
        int ch;
        int pwd_legth = strlen(password);
        int i = 0;
        while ((ch = fgetc(normal_fp)) != EOF) {
            fputc(ch ^ password[i % pwd_legth], encode_fp);
            i = (i++) % 10001;
        }
        fclose(normal_fp);
        fclose(encode_fp);
    }
    int main() {
        char * nomal_path = "D:\\NDK\\class\\files\\beauty.png";
        char * encode_path = "D:\\NDK\\class\\files\\beauty_encode.png";
        char * decode_path = "D:\\NDK\\class\\files\\beauty_decode_.png";
    
        biEncode(nomal_path, encode_path, "ILoveYou");
        biDecode(encode_path, decode_path, "ILoveYou");
    
        system("pause");
        return 0;
    }
    

    结语

    以上就是C++的文件相关操作,只是为了记录知识点,也欢迎大家阅读和错误指正。

    相关文章

      网友评论

          本文标题:C++之文件

          本文链接:https://www.haomeiwen.com/subject/aizuohtx.html