Create a file with current date and time name

Hello !

What I am trying to do is :

1. Create a copy of an existing file and open it.
2. Name the copy with the current date and time.

str = date and time as a string
path = the destination to my file + str + the extension "docx" because I need a Word file
How can I pass the path created that way to the ofstream outputFile() in order to create the copy of the original document with the name I want to ?

This is the code I use :

int main(int argc, char *argv[])
{
ifstream inputFile("C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\RAPORT MEDICAL.docx", ios::in | ios::binary);
inputFile.seekg(0, ios::end);
int length = inputFile.tellg();
inputFile.seekg(0, ios::beg);
string fileContents;
fileContents.resize(length);
inputFile.read(&fileContents[0], length);
inputFile.close();

time_t rawtime;
struct tm * timeinfo;
char buffer[80];

time (&rawtime);
timeinfo = localtime(&rawtime);

strftime(buffer,80,"%d-%m-%Y %I:%M:%S",timeinfo);
std::string str(buffer);
std::cout << str << endl;
std::string path = "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\" + str + ".docx";
std::cout<< path;


ofstream outputFile("C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\RAPORT MEDICAL_copy.docx", ios::out | ios::binary);
outputFile.write(&fileContents[0], length);
outputFile.close();

//system("\"C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\RAPORT MEDICAL_copy.docx\"");
return 0;
}

I would really appreciate some help.
Thanks :)
Last edited on
I think the code could be simplified in parts.

The filename using date/time is ok but for two problems. The character ':' is not permitted. The use of a 12-hour clock could result in a non-unique name, I'd recommend a 24-hour clock.

Also the use of functions can simplify the code, at least it breaks it down into reusable pieces.

Your file copy using an intermediate buffer is ok, though you might consider an alternative. Much of the code below is error handling and message output:
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
52
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

std::string datetime()
{
    time_t rawtime;
    struct tm * timeinfo;
    char buffer[80];

    time (&rawtime);
    timeinfo = localtime(&rawtime);

    strftime(buffer,80,"%d-%m-%Y %H-%M-%S",timeinfo);
    return std::string(buffer);
}

bool filecopy(std::string fnamein, std::string fnameout)
{
    std::ifstream fin(fnamein, std::ios::binary);
    if (!fin)
    {
        std::cout << "could not open input: " << fnamein << '\n';
        return false;
    }
    std::ofstream fout(fnameout, std::ios::binary);
    if (!fout)
    {
        std::cout << "could not open output: " << fnameout << '\n';
        return false;
    }    
    fout << fin.rdbuf();
    return bool(fout);
}

int main()
{
    std::string fnamein  = "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\RAPORT MEDICAL.docx";
    std::string fnameout = "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\RAPORT MEDICAL_copy.docx";
    std::string path = "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\";
 
    fnameout = path + datetime() + ".docx";
    
    bool ok = filecopy(fnamein, fnameout);
    if (ok)
        std::cout << "successful copy\n" << fnamein << "\nto\n" << fnameout << '\n';
    else
        std::cout << "copy failed\n";
        
}
        
Thanks a lot, problem solved now ! :)
Topic archived. No new replies allowed.