Hi I would just like to ask. is it possible to write the output into a text file , if my output is changing every 15 sec.?
Basically i am supposed to be writing a simulation of an OTP device . so we have these 2 programs were in it is called DEVICE and USER. so device is suppose to generate the 4 digit pin that USER needs to use as a 2FApassword to log in . so what i was thinking was maybe i could write the OTP generated password into a text file so that USER can read into that file ??
Yes. 15 seconds is an eternity to a computer, but if your computer is extra slow you can create a ramdisk for it. You should not try to read it immediately, give yourself a small delay, maybe 10 seconds before you try to read it.
There are better ways... you can use 2 threads in 1 program that can talk to each other, or you can use 2 programs and process communication techniques if you want to dig into these topics (they are a bit advanced for new coder).
ohh that's great , so do i just use the normal method of writing into a file ? i mean using ofstream ? cos when i have tried to code it awhile ago , it seems like i can only see the first 4digits that was generated . rest of the OTP generated after 15 seconds didnt showed up .
"There are better ways... you can use 2 threads in 1 program that can talk to each other, or you can use 2 programs and process communication techniques if you want to dig into these topics (they are a bit advanced for new coder)." yes this sounds complicated , thanks for the advice , maybe i will try this once i get better in coding
Open the file, write everything to it, close the file.
if you want to be a smart * you can have program 1 spawn program 2 at this point via
sleep(1000); //wait 1 second, assuming your sleep is in miliseconds
system("program2.exe");
Otherwise program 2 needs to wait (possibly retrying many times) to open the file until it gets what it needs, possibly even checking that it read 100% of the data.
You can also do this:
program1 >> filename //writes the program's output to a text file, use cout!
If you do it that way in a batch/script, you can tie them together as well.
What exactly do you need from it anyway? Do both programs run in a loop for a while and create/consume the files? Or do they run once and quit after a success? If you need them to loop, you need to coordinate them somehow. If you are already using the wall clock time to generate the pad and time the 15 seconds (are you?) you can use it to coordinate also, eg program 1 writes on the 00 second of the min, and program 2 tries to consume on the 5th second of the min, like that maybe...
im not sure if my idea is good . im having a feeling that it is a bad idea...
actually program 1 is suppose to have a log in authentication , username and password should be read from password.txt . so now program 2 is supposed to be the so called program that generates the "OTP digits" every 15 seconds. program 1 needs to key in the digit that the program 2 generated only after it has validated the username/password from the file.
I was thinking if i can write the "OTP digit" inside the password.txt using program 2, Program 1 is able to verify the "OTP digit " from that password.txt .
you mean 2 .txt files ?
text file 1 > for the storage of username and password that program 1 will authenticate
text file 2 > for the storage of the OTP generated data
am i right to say that ?
if yes how will i link file 1 and file 2 in such a way that
username 1 and password 1 and OTP1 will be linked ?
I have a nother question if that's ok . hmmm now i can generate a data and write it into a file for every 15 seconds . the problem is i just want the current data to be in my file .
my file now looks like this
qwer
asdf
zxcv
zvbn ----- reprents current data
but i want it to only have the current data into the text file . so meaning i want it to overwrite every 15 sec . is that possible ? if yes . can please advice ?
#include <iostream>
#include <fstream>
#include <random>
#include <thread>
#include <chrono>
#include <atomic>
#include <ctime>
std::string current_time() { // return the current calendar time as text
// http://en.cppreference.com/w/cpp/chrono/cconst std::time_t now = std::time(nullptr) ;
return std::asctime( std::localtime( std::addressof(now) ) ) ;
}
void periodic_generate_pin( std::string file_name, unsignedint perdiod_in_seconds,
std::atomic<bool>& keep_going ) {
// http://en.cppreference.com/w/cpp/numeric/random
std::mt19937 rng( std::random_device{}() ) ; // the random number engine seeded with a random value
std::uniform_int_distribution<int> pin_distr( 1000, 9999 ) ; // distribution for 4-digit random pin
// http://en.cppreference.com/w/cpp/chrono/durationconst std::chrono::seconds sleep_period(perdiod_in_seconds) ;
// periodically write a random 4-digit pin and current time stamp to the file
// keep doing this till repeatedly till keep_going becomes false
while( keep_going ) {
// note: this overwrites the current contents of the file (if any)
// note: the file would be closed before the thread enters a wait state
if( !( std::ofstream(file_name) << pin_distr(rng) << " @" << current_time() ) )
keep_going = false ; // write to file failed: give up; return immediately
// http://en.cppreference.com/w/cpp/thread/sleep_for
std::this_thread::sleep_for(sleep_period) ; // wait till the time for the next write
}
}
int main() {
const std::string file_name = "OTP.txt" ;
constunsignedint perdiod_in_seconds = 15 ;
// http://en.cppreference.com/w/cpp/atomic/atomic
std::atomic<bool> keep_going { true } ; // set to false to signal to the thread that it should quit
// create a thread to write the pin into the file periodically
// http://en.cppreference.com/w/cpp/thread/thread
std::thread generate_pin_thread( periodic_generate_pin, file_name, perdiod_in_seconds,
std::ref(keep_going) ) ; // keep_going passed as a wrapped reference
// rest of program
// periodic pin generation keeps going on in the background
// note that if we had placed the generated pin into an accessible (atomic) variable,
// we could get the last generated pin by just looking at that variable
// ...
// for example:
std::cout << "press enter to stop program\n" ;
std::cin.get() ;
std::cout << "shutting down gracefully... " << std::flush ;
keep_going = false ;
generate_pin_thread.join() ; // wait for the thread to finish (exit gracefully)
// (may have to wait for a maximum of one sleep period)
std::cout << '\n' ;
}