C++ 如何为文件节省时间?
我有一个将数据保存到文件的程序,我想在该日志上添加当前日期/时间的时间戳,但当我尝试将时间写入文件时,它不会显示,但我写入的其他数据会显示C++ 如何为文件节省时间?,c++,time,C++,Time,我有一个将数据保存到文件的程序,我想在该日志上添加当前日期/时间的时间戳,但当我尝试将时间写入文件时,它不会显示,但我写入的其他数据会显示 #include <iostream> #include <windows.h> #include <fstream> #include <string> #include <sstream> #include <direct.h> #include <stdio.h> #i
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <sstream>
#include <direct.h>
#include <stdio.h>
#include <time.h>
using namespace std;
string header_str = ("NULL");
int main()
{
for(;;)
{
stringstream header(stringstream::in | stringstream::out);
header << "datasdasdasd_";
time_t rawtime;
time ( &rawtime );
header << ctime (&rawtime);
header_str = header.str();
fstream filestr;
filestr.open ("C:\\test.txt", fstream::in | fstream::out | fstream::app | ios_base::binary | ios_base::out);
for(;;)
{
filestr << (header_str);
}
filestr.close();
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串头_str=(“NULL”);
int main()
{
对于(;;)
{
stringstream头(stringstream::in | stringstream::out);
header也许这样行得通
时间与时间
时间(&rawtime);
header我就是这样做的,有一个helper函数,它只提供了所需格式的日期和时间,以便包含在任何输出流中:
#include <time.h>
#include <iostream>
#include <fstream>
using namespace std;
// Helper function for textual date and time.
// DTTMSZ must allow extra character for the null terminator.
#define DTTMFMT "%Y-%m-%d %H:%M:%S "
#define DTTMSZ 21
static char *getDtTm (char *buff) {
time_t t = time (0);
strftime (buff, DTTMSZ, DTTMFMT, localtime (&t));
return buff;
}
int main(void) {
char buff[DTTMSZ];
fstream filestr;
filestr.open ("test.txt", fstream::out|fstream::app);
// And this is how you call it:
filestr << getDtTm (buff) << "Your message goes here" << std::endl;
filestr.close();
return 0;
}
一个非常好的技巧是查看。它很棒,它提供了您所需要的一切,支持简单的输出和格式,并且是可移植的
值得一看。为什么你有一个无限循环?实际上是无限平方:-)抱歉没有“错误C2110:“+”:不能添加两个指针):(好吧,无论如何谢谢你这是有效的xD tyvm(但顺便说一句,你忘了包括)好的观点,责怪gcc如此宽容:-)为后代修正它。
2010-05-05 13:09:13 Your message goes here