2

I'm trying to write a program which outputs a lot of data in separate text files. I want the title of each file to be Poker_Log_timestamp_datestamp.txt However, it doesn't actually create the file, nor does it throw any errors!

Here's the code:

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

How do I make this work? One other thing: If I comment out filename.append(stime) and filename.append(sdate), it works fine.

SOLVED :D The file name cant have any slashes or colons, so i replaced them both with dashes. Here is the working code:

#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");
}
Magicaxis
  • 371
  • 7
  • 16
  • 1
    I don't know what these `_strdate_s` and `_strtime_s` functions are, but if the `cout << ...` line immediately before `log.open` prints what you expect it to print, then your problem is *not* with the construction of the filename. You're not checking for errors in `log.open` -- fstreams do *not* (by default) signal failure by throwing an exception. If you add `if (log.fail()) perror(filename.c_str());` immediately after the `log.open` line, what does it print? (May need to add `#include ` for that to compile.) – zwol Mar 01 '13 at 16:08
  • It says "Poker_log_17:18:13_03/01/13.txt: Invalid Arguement. I thought maybe the '/'s were confusing it, so i removed the date, but it says the same thing about the time-only version. – Magicaxis Mar 01 '13 at 17:19
  • 1
    Colons may be a problem, too, depending on your file system. – Adrian McCarthy Mar 01 '13 at 17:32

2 Answers2

3

The date and time strings may have characters in them (like colons) that may not be legal characters for your file system.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Colons are illegal too?! aaaaaah that'll be it! *starts coding* Cool :D The error message has now changed to `(filename): No such file or directory` – Magicaxis Mar 01 '13 at 17:34
  • Got it :D had to change `fstream log` to `ofstream log` Thank you! – Magicaxis Mar 01 '13 at 17:36
2

Use the following:

   log.open(filename.c_str());

The open method requires a char *, a.k.a. a C-style string, not a C++ std::string.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154