많은 양의 데이터를 별도의 텍스트 파일에 출력하는 프로그램을 작성하려고합니다. 각 파일의 제목을 으로하고 싶습니다. Poker_Log_timestamp_datestamp.txt 그러나 실제로 파일을 만들거나 오류를 발생시키지 않습니다.제목에 타임 스탬프가있는 텍스트 파일 만들기
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main(){
char sdate[9];
char stime[9];
fstream log;
_strdate_s(sdate);
_strtime_s(stime);
string filename = "Poker_Log_";
filename.append(stime);
filename.append("_");
filename.append(sdate);
filename.append(".txt");
cout<<"Filename == "<<filename<<endl;
log.open(filename.c_str());
log<<"\n\nHuman won the tournament.\n";
log.close();
system("Pause");
}
가 어떻게이 일을 어떻게해야합니까 : 여기
코드인가? 다른 한 가지 :filename.append(stime)
및
filename.append(sdate)
을 주석 처리하면 정상적으로 작동합니다.
해결 : D 파일 이름에는 슬래시 또는 콜론이 없으므로 둘 다 대시로 바꿉니다.
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
using namespace std;
int main(){
char sdate[9];
char stime[9];
ofstream log;
_strdate_s(sdate);
_strtime_s(stime);
string filename = "Poker_Log_";
filename.append(stime);
filename.append("_");
filename.append(sdate);
filename.append(".txt");
for(int i = 0; i<filename.length(); ++i){
if (filename[i] == '/' || filename[i] == ':')
filename[i] = '-';
}
log.open(filename.c_str());
if (log.fail())
perror(filename.c_str());
log<<"\n\nHuman won the tournament.\n";
log.close();
system("Pause");
}
이'_strdate_s'와'_strtime_s' 함수는 무엇인지 모르겠지만'log.open' 바로 전에'cout << ...'줄이 출력하기를 기대하는 것을 출력한다면 문제는 * 파일 이름의 구성과 관련이 없습니다. 당신은'log.open'에서 에러를 검사하지 않습니다 - fstream은 예외를 던져서 신호 실패를하지 않습니다. 'log.open' 줄 바로 다음에'if (log.fail()) perror (filename.c_str());를 추가하면 어떻게됩니까? (컴파일 할 때'#include '을 추가해야 할 수도 있습니다.) –
zwol
"Poker_log_17 : 18 : 13_03/01/13.txt : 잘못된 해석. '/ '가 혼란 스러울 것 같아서 날짜를 삭제했지만 시간 전용 버전에 대해서도 똑같은 내용이 표시됩니다. – Magicaxis
파일 시스템에 따라 콜론도 문제가 될 수 있습니다. –