zoukankan      html  css  js  c++  java
  • 文件读取及比较&文件信息保存

    #include <stdio.h>
    #include <stdlib.h>
    //#include <regex.h>
    char* file_name_1 = "D:\testfile.txt";
    char* file_name_2 = "D:\testfile_1.cfg";
    char* file_name_3 = "D:\testfile_flag.txt";
    
    #define ERROR -1
    #define OK 0
    int file_length(char* fileName)
    {
        FILE* fp;
        int file_set_val,file_end_val;
        fp = fopen(fileName, "r");
        if(fp == NULL)
        {
            printf("read file fail
    ");
            return ERROR;
        }
        fseek(fp, 0 , SEEK_SET);
        file_set_val = ftell(fp);
        fseek(fp, 0 , SEEK_END);
        file_end_val = ftell(fp);    
        printf("file length is %d. 
    file_set_val:%d file_end_val:%d 
    ", file_end_val-file_set_val,file_set_val, file_end_val);
        return file_end_val-file_set_val;
    }
    
    
    int read_file(char* fileName, char* content, int len)
    {
        FILE* fp;
        fp = fopen(fileName, "r");
        if(fp == NULL)
        {
            printf("read file fail
    ");
            return ERROR;
        }
        fread(content, len, 1, fp);
        printf("read content is:%s
    ", content);
        return 0;
    }
    
    int back_para_increase(char* fileName, int flag)
    {
        FILE* fp;    
        fp = fopen(fileName, "ab");
        if(fp == NULL)
        {
            printf("read file fail
    ");
            return ERROR;
        }
        if(flag)
        {
            fputc('1', fp);
        }    
        fclose(fp);    
    }
    
    int file_compare()
    {
        char* content_1;
        char* content_2;
        int len = 0, flag_val = 0;
        
        len = file_length(file_name_1);
        content_1 = (char*)malloc(len);
        memset(content_1, 0, len);
        read_file(file_name_1, content_1, len);
        
        len = file_length(file_name_2);
        content_2 = (char*)malloc(len);
        memset(content_2, 0, len);
        read_file(file_name_2, content_2, len);
        
        if(strcmp(content_1, content_2))
        {
            back_para_increase(file_name_3,1);
            flag_val = file_length(file_name_3);
            printf("back_para_increase:%d
    ",flag_val);
        }
        free(content_1);
        free(content_2);
    }
    
    int main()
    {
        printf("***************************
    ");
        file_compare();
        return 0;
    }
  • 相关阅读:
    关于 虚拟化 的 我的看法
    .Net 一开始就不应该搞 .Net Core
    我提出了一个 Lean Html 5 的 概念 和 标准
    我发起了一个 用 C# 写 的 浏览器 开源项目 HtmlCore
    我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore
    我发起了一个 ILBC 的 子项目 ILBC Studio
    我发起了一个 ILBC 的 子项目 EScript
    ILBC 规范
    微编程 时代 已经到来
    C++ 是 编程界 的 背锅侠
  • 原文地址:https://www.cnblogs.com/liuyang92/p/6602516.html
Copyright © 2011-2022 走看看