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;
    }
  • 相关阅读:
    python爬取北京政府信件信息03
    Python3.7 lxml引入etree
    python爬取北京政府信件信息02
    python爬取北京政府信件信息01
    2020.12.2收获
    2020.12.1收获
    2020.11.30收获
    2020.11.29收获
    2020.11.28收获
    2020.11.27收获
  • 原文地址:https://www.cnblogs.com/liuyang92/p/6602516.html
Copyright © 2011-2022 走看看