?
Solved

File Copy in C++

Posted on 2003-03-07
3
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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