?
Solved

File Copy in C++

Posted on 2003-03-07
3
Medium Priority
?
248 Views
Last Modified: 2010-04-01
I have a file log.txt
how can I copy this file and name it the current date and time?

Thanks
0
Comment
Question by:oliverUK
3 Comments
 
LVL 3

Accepted Solution

by:
cmaryus earned 210 total points
ID: 8088325
char szOldFileName[] = "File.log";
     char szNewFileName[_MAX_FNAME];

     SYSTEMTIME sysTime;
     GetSystemTime(&sysTime);

     // the name will be:
     // File_YYYY_MM_DD_HH_MM_SS.log
     sprintf(szNewFileName, "File_%0.4d_%0.2d_%0.2d_%0.2d_%0.2d_%0.2d.log",
                    sysTime.wYear,
                    sysTime.wMonth,
                    sysTime.wDay,
                    sysTime.wHour,
                    sysTime.wMinute,
                    sysTime.wSecond);

     //now use the Win Api CopyFile
     CopyFile(szOldFileName, szNewFileName, FALSE);
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 8088994
If you want to do this using only the standard ( therefore it'll be portable ) :

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <ctime>
#include <sstream>

using namespace std;

int main()
{
     time_t rawtime;
     struct tm * info;
     time ( &rawtime );
     info = localtime ( &rawtime );

     stringstream tmp;
     tmp << "File_" << info->tm_mday
          << '-' << info->tm_mon
          << '-' << info->tm_year
          << '_' << info->tm_hour
          << '-' << info->tm_min
          << '-' << info->tm_sec
          << ".log";

     const string strOld = "File.log";
     string strNew = tmp.str();

     copy(istream_iterator<char>( ifstream(strOld.c_str()) ), istream_iterator<char>(), ostream_iterator<char>( ofstream(strNew.c_str()) ));

     return 0;
}

0
 
LVL 1

Expert Comment

by:MatrixDud
ID: 8089079
Here's an easier way:


char sTimeDate[26];

struct tm *newtime;
__int64 ltime;

_time64( &ltime );
newtime = _localtime64( &ltime );
strftime( sTimeDateFile, 25, "%b %d %Y, %H:%M.txt", newtime );

CopyFile( "log.txt", sTimeDateFile, false );


For more info on the date/time formatting follow this link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question