// This file contains the logic necessary to open a 'unique number' file // grab the number it contains, and then increment the number inside // the file before closing the file. #include #include #include #include "unique_id.h" #include "errors.h" int get_unique_id() { static char* filename = "unique_number"; ifstream f_in; ofstream f_out; int current; // the current value of the unique number // read in the current value of the unique number f_in.open(filename); if (!f_in) { strcpy(fatal_err_function, "get_unique_number"); sprintf(fatal_err_msg, "unable to open %s to read", filename); fatal_error(); } f_in >> current; if (current < 1) { strcpy(fatal_err_function, "unique_id::initialize"); sprintf(fatal_err_msg, "file %s has invalid value %d", filename, current); fatal_error(); } f_in.close(); // write out the next value of the unique number to the file f_out.open(filename); if (!f_out) { strcpy(fatal_err_function, "get_unique_number"); sprintf(fatal_err_msg, "unable to open %s to write", filename); fatal_error(); } f_out << (current+1); f_out.close(); return current; }