I need the program to create a folder with the current date installed on the PC. Here is the code that I wrote and the error message that results from the execution of the code.

#include "time.h" #include <fstream> using namespace std; char buffer[80]; char* filename ( ) { time_t timess; struct tm * timeinfo; time ( &timess ); char *buff2 = buffer; timeinfo = localtime ( &timess ); strftime ( buff2, 80, "%y%m%d%", timeinfo ); strcat ( buff2, ".txt" ); return ( buff2 ); } 

Error 1 error C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f: \ documents \ visual studio 2013 \ projects \ main \ time file creator \ main.cpp 12 1 time file creator

Tell me what am I doing wrong? And if possible, describe how to make that from another folder, all the files that are in it, moved to this folder automatically? (and only 1 file type was left)

Write in detail, because I'm still new to this.

  • No need to link to the code in external sources. Post it right here in question. Markdown markup can be used for formatting: www.stackoverflow.com/editing-help - Nick Volynkin
  • The compiler says that using localtime is not safe, to ignore add the _CRT_SECURE_NO_WARNINGS diff or use the localtime_s function. - Vladimir Gamalyan
  • Well, in the future I will spread everything here. - Bagger
  • I tried to add to the beginning of the program # define _CRT_SECURE_NO_WARNINGS, the same thing happened. Used localtime_s there was another error. "Error 1 error C2660: localtime_s: the function does not accept 1 argument" - Bagger
  • one
    Something a bit here from c++ . - αλεχολυτ

1 answer 1

This is not a mistake, this is a warning: you are using a potentially unsafe function ...

If you understand what you are doing and take responsibility, ignore the warning (or define this macro that is being asked for).

Or use the functions _s - the same localtime_s , in which you really need to pass two parameters - pointers to tm and time_t .

Like that:

 char buffer[80]; char* filename() { time_t timess; struct tm timeinfo; time(&timess); char *buff2 = buffer; localtime_s(&timeinfo, &timess); strftime(buff2, 80, "%Y%m%d", &timeinfo); strcat_s(buff2, 80, ".txt"); return (buff2); } int main() { cout << "start\n"; cout << filename() << endl; return 0; } 
  • This is a mistake, take a closer look at the text. The studio believes that it has the right to give such errors - ixSci
  • @ixSci Is it not worth setting just "treat all warnings as errors"? I have a key /W4 gives just as Warning. - Harry
  • I have a studio without WX for 2015 and with W3 it gives exactly the error. And 2, one more on strcat - ixSci
  • Found the reason - key / sdl - ixSci
  • Well, this code has earned me without errors and warnings. I would like to know how to create a file with all this now? I only have the current date. Txt writes, but does not create anything anywhere ... - Bagger