设为首页 加入收藏

TOP

C语言中文件操作、预处理命令讲解(一)
2017-11-22 06:07:02 】 浏览:447
Tags:言中 文件 操作 处理 命令 讲解

1. 文件操作

文件分为两部分: 控制信息 和 内容信息

文本文件 和 图片 本质上都是 二进制文件,只是控制信息 不同而已

读文件的流程: 磁盘(得到二进制文件)》 文件缓冲区》应用程序内存空间

在widows看到的各种各样的文件比如jpg,png,avi各种不同类型的文件只是在 从磁盘读到文件缓冲区的时候他会根据文件的控制信息进行相应的编解码

C语言 二进制文件和文本文件读写的区别:

二进制:写文本 ‘\n’-> \r\n

二进制:读文本 \r\n -> \n

1. read 文件、写文件

////////////////read文件
int main()
{
    char *path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\friends.txt";

    FILE *fp = fopen(path, "r");

    char buff[500];
    while (fgets(buff, 50, fp))
    {
        printf("%s", buff);
    }

    fclose(fp);
    system("pause");
    return 0;
}
/////////////////////////////////写文件//////////////////
int main() {

    char *path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\friends_write2.txt";
    // 如果不存在,会创建
    FILE *fp = fopen(path, "w");

    if (fp == NULL) {
        printf("failed。。。。");
        return 0;
    }

    char *text = "你好,WTF?";
    fputs(text, fp);
    fclose(fp);


    system("pause");
    return 0;
}

2. 读写 二进制文件

///////////////////////////读写二进制文件
int main() {
    char * read_path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\LogViewPro.exe";
    char * write_path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\LogViewPro_write.exe";

    //read :rb 的b是以二进制的形式,没有b就是以文本的形式
    FILE * read_fp = fopen(read_path, "rb");
    //write
    FILE * write_fp = fopen(write_path, "wb");
    char buff[50];
    int len = 0;
    // 从read_fp里面读每次50个sizeof(char)大小的文件
    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;
}

3. size of the file

int main() {
    char *read_path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\liuyan.png";

    FILE *fp = fopen(read_path, "r");
    if (fp == NULL)
    {
        return 0;
    }
    // 设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。
    fseek(fp, 0, SEEK_END);
    // 返回给定流 stream 的当前文件位置。
    long  filesize = ftell(fp);

    printf("%ld \n", filesize);

    system("pause");
    return 0;
}

4. 文本文件加解密

// ^ 异或运算 7(0111)
// 1001 ^ 0111 = 1110
// 1110 ^ 0111 = 1001 

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 = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\friends.txt";
    char * encode_path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\friends_encode.txt";
    char * decode_path = "G:\\0_preClass\\NDK\\class\\Ls_C5\\files\\friends_decode.txt";
    //encode(nomal_path, encode_path);
    decode(encode_path, decode_path);
    system("pause");
    return 0;
}

2. 预处理命令

预处理命令:

首先要了解C语言的执行流程,C 语言首先将主程程序的各个元文件(.c .cpp)通过编译生成.obj ,生成之后,然后将各个目标文件obj文件由

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言如何编译动态库与静态库? 下一篇C语言指针部分的知识

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目