Problem with program

jbronen 0 Tallied Votes 516 Views Share

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;
}
jbronen 0 Newbie Poster

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

Adak 419 Nearly a Posting Virtuoso

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.

jbronen 0 Newbie Poster

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!

nezachem 616 Practically a Posting Shark

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

jbronen 0 Newbie Poster

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.