954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Problem with program

0
By jbronen on Jul 20th, 2010 11:01 pm

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

jbronen
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

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!

jbronen
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
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

jbronen
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You