How do I create files with the current time?

I want to create a series of files in the "log" directory, where each file has a name based on the runtime. And in each of these files, I want to store some log data for my program, like a function prototype that is in effect, etc. I usually use the hard fopen ("log / ***", "a") method, which is not suitable for this purpose. And I just write the timestamp function:

char* timeStamp(char* txt){
  char* rc;
  char timestamp[16];
  time_t rawtime = time(0);
  tm *now = localtime(&rawtime);

  if(rawtime != -1) {
     strftime(timestamp,16,"%y%m%d_%H%M%S",now);
     rc = strcat(txt,timestamp);
  }
  return(rc);
}

      

But I don't know what to do next. Please help me with this!

+2


source to share


4 answers


Declare an array char

large enough to store 16 + "log/"

(so a total of 20 characters) and initialize it to "log/"

, then use strcat()

or something to do with the addition of the time string returned by your function to the end of your array.
And there you go!

Notice how string addition works: your array char

is 16 characters, which means you can enter 15 characters plus a null byte. It is important not to forget about this. If you want a 16 character string, you need to declare it as char timestamp[17]

. Note that it "log/"

is a 4 character string, so it takes 5 characters (one for the nul byte at the end), but strcat()

will overwrite starting at the zero byte at the end, so you end up with the correct number. Don't read the nul terminator twice, but more importantly, don't forget it. Debugging is a much bigger problem.



EDIT: While we're at it, I'm reading your code wrong. I thought it just returned the string with time, but it seems like it is adding time to the passed string. This is probably better than I thought you were doing. However, if you want, you can just make the function do all the work - it puts "log/"

on the string before it puts the timestamp. It's not that hard.

+3


source


How about this:

#include <stdio.h>
#include <time.h>

#define LOGNAME_FORMAT "log/%Y%m%d_%H%M%S"
#define LOGNAME_SIZE 20

FILE *logfile(void)
{
    static char name[LOGNAME_SIZE];
    time_t now = time(0);
    strftime(name, sizeof(name), LOGNAME_FORMAT, localtime(&now));
    return fopen(name, "ab");
}

      

You would use it like this:



FILE *file = logfile();
// do logging
fclose(file);

      

Be aware that localtime()

it is not thread safe!

+3


source


It looks like you basically chose to do this - create the file that you describe:

char filename[256] = "log/";
timeStamp( filename );
f = fopen( filename, "a" );

      

Or do you want to do something else?

+1


source


Steps to create (or write) a sequential access file in C ++:

1.Set the name of the stream variable:

ofstream fout;  //each file has its own stream buffer

      

ofstream is short for a stream of output files fout is the name of a stream variable (and can be any legal C ++ variable name). The naming of a stream variable "fout" is useful in remembering that information will be "deleted" into a file.

2.Open the file:

fout.open (filename, ios :: out);

fout is the name of the stream variable, previously declared "scores.dat" is the name of the file ios :: out is the steam mode (your compiler may not require you to specify the thread mode.)

3. Write the data to the file:

fout<<grade<<endl;
fout<<"Mr";

      

The data must be separated by spaces or end-of-line characters (carriage return), or the data will run together in a file and be unreadable. Try to save the data to the file in the same way that you display it on the screen.

If the header file iomanip.h is used, you can use the familiar formatting commands to output the file.

fout<<setprecision(2);
fout<<setw(10)<<3.14159;

      

4. Close the file:

fout.close( );

      

Closing the file writes any data remaining in the buffer to the file, releases the file from the program, and updates the file directory to reflect the new file size. As soon as your program finishes accessing the file, the file should be closed. Most systems close any data files when the program exits. If data remains in the buffer when the program exits, you may lose that data. Don't take a chance - close the file!

+1


source







All Articles