i try to make program that write timestamp to file with c++ here is my code :
#include <time.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
int main()
{
fstream outfile;
outfile.open("time.txt",ios::out|ios::app);
string savetime;
for(;;){
time_t t = time(NULL);
savetime=asctime(localtime(&t));
cout<<savetime;
outfile << savetime << endl;
sleep(1);
}
}
result of this code is
Sun Dec 06 01:28:17 2020
Sun Dec 06 01:28:18 2020
Sun Dec 06 01:28:19 2020
Sun Dec 06 01:28:20 2020
it has new line every lines. i have try to remove endl
at outfile << savetime << endl;
to remove new lines but file wont save new timestamp. my goal is to make output like this
Sun Dec 06 01:28:17 2020
Sun Dec 06 01:28:18 2020
Sun Dec 06 01:28:19 2020
Sun Dec 06 01:28:20 2020
thanks in advance.
#include <iostream>
#include <fstream>
#include <ctime>
#include <unistd.h>
using namespace std;
int main()
{
fstream outfile;
outfile.open("time.txt",ios::out|ios::app);
char savetime[100];
for(;;){
time_t t = time(NULL);
strftime(savetime, sizeof(savetime), "%A %c", localtime(&t));
cout<<savetime;
outfile << savetime<<endl;
sleep(1);
}
}
thanks to user4581301
from savetime=asctime(localtime(&t));
to strftime(savetime, sizeof(savetime), "%A %c", localtime(&t));