1

I need help with my log file. Every time I run the tool currently it gives logs on the same file, I need to add a code which can help me create a new file each time the connection is made. Help would be extremely appreciated.

_mkdir(Path.c_str());

std::string FullFileName;
FullFileName.append(Path);
FullFileName.append(FileName);

::_sopen_s(&m_FileHandle, FullFileName.c_str(), _O_CREAT | _O_WRONLY | _O_BINARY, _SH_DENYWR, _S_IREAD | _S_IWRITE);
return m_FileHandle != -1 ? ::_lseek(m_FileHandle, 0, SEEK_END) : -1;
east1000
  • 1,240
  • 1
  • 10
  • 30
Afaq
  • 13
  • 4
  • 2
    `...help me create a new file each` **time** `the connection is made`. There is a hint in your question that will solve your problem. – Sani Huttunen Sep 06 '21 at 13:49
  • I do that with my logging. Make some part of the log file name contain the date and time of creation of the file. – drescherjm Sep 06 '21 at 13:50
  • Can't you add something unique to the filename? eg.: the time? So FileName+=time() – Botond Horváth Sep 06 '21 at 13:52
  • or just a counter. – user253751 Sep 06 '21 at 13:54
  • @drescherjm does the log restart when you re run the file and gets saved to a new txt file? – Afaq Sep 06 '21 at 14:00
  • Since the time has changed you will get a new filename each time the program is run. – drescherjm Sep 06 '21 at 14:09
  • Thats ok, can you share the code please? @drescherjm – Afaq Sep 06 '21 at 14:20
  • There are many ways to get a timestamp into a std::string in this question: [https://stackoverflow.com/questions/9527960/how-do-i-construct-an-iso-8601-datetime-in-c](https://stackoverflow.com/questions/9527960/how-do-i-construct-an-iso-8601-datetime-in-c) and this question: [https://stackoverflow.com/questions/36927626/outputting-date-in-iso-8601-format](https://stackoverflow.com/questions/36927626/outputting-date-in-iso-8601-format) – drescherjm Sep 06 '21 at 14:26

1 Answers1

2

Add

auto end=std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(end);
FileName+=std::ctime(&time);

before FullFileName.append(FileName); Will add the time to the filename so it will be unique. (If you want to start it multiple times a second you can add miliseconds)

(also you have to #include <chrono>)

Botond Horváth
  • 929
  • 2
  • 15