C++ 流不应创建新行

C++ Streams Should Not create New Line

我尝试制作使用 C++ 将时间戳写入文件的程序,这是我的代码:

#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);
    }
    
}

这段代码的结果是

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

每行换行。我已尝试在 outfile << savetime << endl; 处删除 endl 以删除新行,但文件不会保存新的时间戳。我的目标是做出这样的输出

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

提前致谢。

#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);
    }
}

感谢 user4581301

savetime=asctime(localtime(&t));strftime(savetime, sizeof(savetime), "%A %c", localtime(&t));