Thursday, August 17, 2017

Making a Unique File name

I overheard one fellow asking another fellow for help in creating unique file names.  He wanted a base name with a random number attached at the end.  This happens when you are doing logging and you do not want to over-write the old file or have to delete the old file before you create a new one.  Well, you do not need a random number generator.  You can just add a number to the file name that is different each time you run the program that generates the log file.  Imagine running a test program several times during a day and you want to harvest all the log files.

Below is a small program that generates files with names like this:
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt

Epoch time in C is the total number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC (Greenwich mean time).  With 5 digits the numbers will not "roll over" until after 100,000 seconds, meaning 27.7 hours.   The randomness comes from running the logging program at different times.  Usually after 8 hours of testing you harvest the log files for study and clear out the old files.

The simple program in C that has this output follow.  You can copy and paste this code and run it to test it.  You can incorporate the ideas from this code into your own code when you write a file that will be written repeatedly during the day.  This gives you multiple copies of the file, which is important when the output to the file is changing from iteration to iteration.  I hope you find this helpful.  I license this code to you free of charge via the MIT license, which appears at the very end.

Robert
-----------------------------------------------
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // sleep()

// compile with gcc file_unique_id.c -o file_unique_id

int main ()
{
  char* base_name = "test_file";
  char* extention = ".txt";
  // these string lengths are arbitrary
  char  long_string[200]; // holds file name
  char  short_string[100];// hold time in a string
  FILE* fd;
  int   chopped_time;
  time_t test_time;
  int index;
  for (index = 0 ; index < 5; ++index)
  {
    test_time = time(0); // get epoch time
    chopped_time = (test_time % 100000);  // keep the smallest 5 digits
    // add those 5 digits to the base file name
    sprintf(long_string, "test_file_%d", chopped_time);
    // add the file extension
    // long_string is now the file name with a unique 5 digit number
    strcat(long_string, extention);
    // short_string holds the full epoch time value
    sprintf(short_string, "%d\n", test_time);
    // log short_string to file whose name is in long_string
    fd = fopen(long_string, "w");
    fwrite(short_string, strlen(short_string), 1, fd);
    fclose(fd);
    sleep(3);
  }
  return 0;
}

/**
 * Sample outputs of this program
./file_unique_id
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt

cat test_file_27023.txt
1503027023
**/
Copyright 2017 Robert Canright
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

No comments:

Post a Comment

Comments require my approval before they appear. Be patient. It will take a day or two for your comment to appear.