2010-05-05 6 views
1

나는 파일에 데이터를 저장하는 프로그램을 가지고 있고 그 로그에 현재 날짜/시간의 타임 스탬프를 넣고 싶지만 파일에 시간을 쓰려고 할 때 그것은 나타나지 않지만 다른 데이터는 내가 쓰는 의지.파일에 시간을 절약하려면 어떻게해야합니까?

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

누구나이 문제를 해결하는 방법을 알고 계십니까?

+3

왜 무한 루프가 있습니까? 오 잘 어쨌든이의 xD의 tyvm 작동 음 –

+3

무한대 – paxdiablo

+1

무한 = 재미의 xD에게 :-) 실제로 – blood

답변

3

작동 이 내용으로 파일 test.txt를 만듭니다

2010-05-05 13:09:13 Your message goes here 
+0

감사 :( "두 개의 포인터를 추가 할 수 없습니다 : '+'오류 C2110"(그러나 BTW 당신은 ​​# 깜빡 – blood

+0

좋은 점은 용서를 위해 gcc를 비난하는 것입니다 :-) 미래 세대를 위해 고쳐졌습니다. – paxdiablo

0

어쩌면이 일 것?

써서 time_t rawtime을;

시간 (& rawtime); 헤더 < < "시간 : % s"+ ctime (& rawtime);

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

:

희망이이 난 그냥 모든 출력 스트림에 포함시키기 위해 당신이 원하는 형식으로 날짜와 시간을 준 도우미 기능, 그것을 할 거라고 어떻게

+0

미안 더 제곱 없습니다 를 포함하십시오) – blood

0

정말 좋은 팁이 boost\date_time library을 확인하는 것입니다. 그것은 위대합니다. 필요한 모든 것이 간단한 출력과 포맷을 지원하고 휴대 할 수 있습니다.

가치가 있습니다.

관련 문제