Hey,
I'm writing a program that takes files and renames them to include a timestamp of the current time on them in c++. I'm pretty new to c++ (and programming in general) and any help or advice would be greatly appreciated.

So far I have this:

When I compile it in Code::Blocks in Windows XP, I get the following message in the command prompt that is launched:

Error renaming file: invalid argument.

Any ideas?

Thanks,
jbronen

#include <iostream>
#include <cstdio>
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <string.h>

using namespace std;

int main ()
{
    time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  cout << asctime(timeinfo) << endl;


    int result;
  const char* oldname ="TEST.TXT";
  const char* newname =asctime(timeinfo);
  result = rename( oldname , newname );
  if ( result == 0 )
    puts ( "File successfully renamed" );
  else
    perror( "Error renaming file" );
  return 0;
}

Hey,
I'm writing a program that takes files and renames them to include a timestamp of the current time on them in c++. I'm pretty new to c++ (and programming in general) and any help or advice would be greatly appreciated.

So far I have this:

When I compile it in Code::Blocks in Windows XP, I get the following message in the command prompt that is launched:

Error renaming file: invalid argument.

Any ideas?

Thanks,
jbronen

Also, I've been trying to edit this program so that it adds the timestamp to the file name, (rather than renaming it with the time), but I keep on getting problems with the differences between strings and const char* variables. Any suggestions?

Thanks again,
jbronen

Are you trying to change a CONST value?

If you add something to the file name, you are renaming the file. If you just want to "touch" some file values, I believe you do that through the FILE pointer struct, associated with that file name.

by "touch" I mean the old "touch" utility - changing file creation time/date, file access time/data, etc.

Edited 6 Years Ago by Adak: n/a

Are you trying to change a CONST value?

If you add something to the file name, you are renaming the file. If you just want to "touch" some file values, I believe you do that through the FILE pointer struct, associated with that file name.

by "touch" I mean the old "touch" utility - changing file creation time/date, file access time/data, etc.

Hey,
I am actually trying to rename the file, but I keep on getting this error message in the command prompt, and nothing I've tried seems to work!

The string returned by asctime contains an embedded colons and a newline, which are forbidden in Windows filenames.

Edited 6 Years Ago by nezachem: n/a

The string returned by asctime contains an embedded colons and a newline, which are forbidden in Windows filenames.

Thank you; I was able to fix the program