Time and Date as Text File Name

Hello, I'm new to this forum, but I have been using it's advice and it's been great. Anyways, I've been writing a journal program. I've been able to get the filename to be the user's input although I would like the filename to be the date and time (%c) so that the user may see when was the entry made. Here's what I have so far...

*Note: I am practically a beginner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main(){

    string words;
   
    cout << endl;
    cout << endl;
    
    cout << "Welcome to the JOURNAL App!";
    
    int timeanddate = get time/date; //Here's where my problem is...
    
    ofstream myfile;
    myfile.open (timeanddate); //and also here...
    
    cout << endl;
    cout << endl;
    
    cout << "When you've finished typing, hit * then ENTER." << endl;;
    cout << "Start typing here:";
    cout << endl;
    
    getline(cin, words, '*');
    
    myfile << endl;
    myfile << words;
    
    myfile.close();
    return 0;
}
Last edited on
NVM, I'm sorry. Just solved it, with *cough cough* another forum.

Here's my final code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main(){

    string words;
   
    cout << endl;
    cout << endl;
    
    string dateTime;
    
    time_t rawtime;
    struct tm * timeinfo;
    char buffer [80];
    time ( &rawtime );
    
    timeinfo = localtime ( &rawtime );
    strftime (buffer,80,"%c.txt",timeinfo); // NOTE: the ":" will not work, file name was "03-08-10 15" after output. Try using a different symbol, I'm going to use "+" in mine.
    dateTime = buffer;
    
    // %m-%d-%y %H:%M
    
    FILE * pFile;
    pFile = fopen (dateTime.c_str(),"w");
    fclose (pFile);
    
    cout << "Welcome to the JOURNAL App!";
        
    ofstream myfile;
    myfile.open (dateTime.c_str());
    
    cout << endl;
    cout << endl;
    
    cout << "When you've finished typing, hit * then ENTER." << endl;;
    cout << "Start typing here:";
    cout << endl;
    
    getline(cin, words, '*');
    
    myfile << endl;
    myfile << words;
    
    myfile.close();
    return 0;
}
Topic archived. No new replies allowed.