snmpEngineBoots & snmpEngineID数据存储到非易失性存储设备
#include <stdio.h> #include <stdlib.h> #include <string.h> int regenerateID() { return 10; } int read_snmpEngine_file(char* file_name, int* snmpEngineBoots, int* snmpEngineID) { FILE* fp; fp = fopen(file_name, "r"); fscanf(fp, "%d, %d", snmpEngineBoots, snmpEngineID); fclose(fp); return 0; } int update_snmpEngine_file(char* file_name, int* snmpEngineBoots, int* snmpEngineID) { FILE* fp; fp = fopen(file_name, "w+"); fprintf(fp, "%d, %d", *snmpEngineBoots + 1, *snmpEngineID); fclose(fp); } int get_snmpEngine_data(char* file_name, int* snmpEngineBoots, int* snmpEngineID) { FILE* fp; fp = fopen(file_name, "r"); if(fp == NULL) { *snmpEngineBoots = 0; *snmpEngineID = regenerateID(); } else { fclose(fp); read_snmpEngine_file(file_name, snmpEngineBoots, snmpEngineID); } remove(file_name); update_snmpEngine_file(file_name, snmpEngineBoots, snmpEngineID); return 0; } int main() { char* file_name = "snmp_engine.cfg"; int snmpEngineID = 0, snmpEngineBoots = 0; get_snmpEngine_data(file_name, &snmpEngineBoots, &snmpEngineID); printf("%d %d\n", snmpEngineBoots, snmpEngineID); }