hello all need help in solving this problem
print current time in a file for every 1min.
thank in advance.:)
hello all need help in solving this problem
print current time in a file for every 1min.
thank in advance.:)
: the simplest, robust approach is to run a small loop (in a background thread for GUI apps) that formats the current time and appends it to a file once every minute. was on the right track mentioning threads and file streams; asked for specifics, so below is a clear pattern using the C++ standard library (portable if you keep to std::*). The example uses a stop flag so the logger can exit cleanly; it opens the file in append mode each iteration so data is flushed even after a crash.
#include <fstream>
#include <thread>
#include <atomic>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
int main() {
std::atomic<bool> stop{false};
std::thread logger([&stop]() {
while (!stop.load()) {
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm;
#ifdef _MSC_VER
localtime_s(&tm, &t);
#else
localtime_r(&t, &tm);
#endif
std::ostringstream oss;
oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
std::ofstream ofs("time_log.txt", std::ios::app);
if (ofs) ofs << oss.str() << '\n';
for (int i = 0; i < 60 && !stop.load(); ++i)
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
// example: run for 5 minutes then stop
std::this_thread::sleep_for(std::chrono::minutes(5));
stop.store(true);
logger.join();
} Notes and tips: enable C++11 or later in your project so <thread> and <chrono> are available; use localtime_s on MSVC for thread safety (the conditional above picks the right call). If the program must run without a logged-in user, use Task Scheduler or a Windows Service instead of a console app. Use absolute paths or check the working directory to ensure the log file lands where you expect.
Jump to Post— kvprajapati 1,826trinity_neo,
Think about thread and file stream.
trinity_neo,
Think about thread and file stream.
It is very easy, what's exactly your problem?
i am a newbie to visual there any pre built functions which i can use? and is the coding similar to borland or DEV c++.
thanks in advance
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.