栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言学习笔记——(六)文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言学习笔记——(六)文件

注:编码工具是CLion+Cygwin64

目录

文件的读写

读文件

写文件

文件的复制

获取文件的大小

文件的简单加解密

加解密算法:异或

加密

解密


文件的读写

        fopen函数,以特定模式打开指定文件。

        模式:r(读)、w(写)、rb(二进制读)、wb(二进制写)、rw(读写)。

        读文件的时候,如果指定文件不存在会报错。

        写文件的时候,如果指定文件不存在会创建文件。

        fclose函数,文件操作完成后,要调用此函数关闭文件,释放资源。

        FILE是一个结构体,里面记录了文件的各种信息。

读文件
#include 

int main(){
    char* readFilePath = "D:\CFile\静夜思.txt";
    FILE * file= fopen(readFilePath, "rw");// 用r也行
    char buffer[128];
    while(fgets(buffer, 128, file))
    {
        printf("%s", buffer);
    }
    fclose(file);

    return 0;
}

输出:

床前明月光,疑是地上霜。
举头望明月,低头思故乡。

写文件
#include 

int main(){
    char* writeFilePath = "D:\CFile\WriteTest.txt";
    FILE * file= fopen(writeFilePath, "w");
    if(!file){
        printf("error");
    }
    char * str = "this is from C";
    fputs(str, file);
    fclose(file);

    return 0;
}

文件的复制
#include 

int main() {
    char *readFilePath = "D:\CFile\静夜思.txt";
    char *writeFilePath = "D:\CFile\静夜思副本.txt";
    FILE *readFile = fopen(readFilePath, "rb");
    FILE *writeFile = fopen(writeFilePath, "wb");
    char buffer[512];
    unsigned long len;
    while ((len = fread(buffer, sizeof(char), sizeof(buffer)/sizeof(char), readFile)) != 0) {
        fwrite(buffer, sizeof(char), len, writeFile);
    }
    fclose(readFile);
    fclose(writeFile);

    return 0;
}

获取文件的大小

        fseek将指针从文件头移动到文件尾。

        ftell计算文件的大小。

#include 

int main() {
    char *filePath = "D:\CFile\静夜思.txt";
    FILE *file = fopen(filePath, "r");
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    printf("文件大小为:%ld字节n", size);
    fclose(file);
    return 0;
}

输出:

文件大小为:74字节

文件的简单加解密

加解密算法:异或

        一个数连续两次异或另一个数得到它自己。

#include 

int main() {
    int num = 9;
    printf("num = %dn", num);
    int seed = 8;
    int encodeNum = num ^seed;
    printf("encodeNum = %dn", encodeNum);
    int decodeNum = encodeNum ^seed;
    printf("decodeNum = %dn", decodeNum);
    if (num == decodeNum)
        printf("decodeNum == num");
    else
        printf("decodeNum != num");
}

输出:

num = 9
encodeNum = 1
decodeNum = 9
decodeNum == num

加密
#include 

int main() {
    char *filePath = "D:\CFile\静夜思.txt";
    char *encodeFilePath = "D:\CFile\加密后的静夜思.txt";
    FILE *file = fopen(filePath, "r");
    FILE *encodeFile = fopen(encodeFilePath, "w");
    int seeds[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int count = 0;
    int c;
    while ((c = fgetc(file)) != EOF) {
        fputc(c ^ *(seeds + (count % 9)), encodeFile);
    }
    fclose(file);
    fclose(encodeFile);
    return 0;
}

加密后的内容:

介䈌癏睉䄈旐癮䝱幋蝝⁃广䥵睚癏睉式䥵灜甄帠⁃

解密
#include 

int main() {
    char *encodeFilePath = "D:\CFile\加密后的静夜思.txt";
    char *decodeFilePath = "D:\CFile\解密后的静夜思.txt";
    FILE *encodeFile = fopen(encodeFilePath, "r");
    FILE *decodeFile = fopen(decodeFilePath, "w");
    int seeds[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int count = 0;
    int c;
    while ((c = fgetc(encodeFile)) != EOF) {
        fputc(c ^ *(seeds + (count % 9)), decodeFile);
    }
    fclose(encodeFile);
    fclose(decodeFile);
    return 0;
}

解密后的内容:

床前明月光,疑是地上霜。
举头望明月,低头思故乡。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433397.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号