Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a text file with a timestamp in the title
    primarykey
    data
    text
    <p>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 <em>Poker_Log_timestamp_datestamp.txt</em> However, it doesn't actually create the file, nor does it throw any errors!</p> <p>Here's the code:</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;string&gt; 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&lt;&lt;"Filename == "&lt;&lt;filename&lt;&lt;endl; log.open(filename.c_str()); log&lt;&lt;"\n\nHuman won the tournament.\n"; log.close(); system("Pause"); } </code></pre> <p>How do I make this work? One other thing: If I comment out <code>filename.append(stime)</code> and <code>filename.append(sdate)</code>, it works fine.</p> <p>SOLVED :D The file name cant have any slashes or colons, so i replaced them both with dashes. Here is the working code:</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;string&gt; #include &lt;cstdio&gt; 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&lt;filename.length(); ++i){ if (filename[i] == '/' || filename[i] == ':') filename[i] = '-'; } log.open(filename.c_str()); if (log.fail()) perror(filename.c_str()); log&lt;&lt;"\n\nHuman won the tournament.\n"; log.close(); system("Pause"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload