renaming a file based of a time

I am trying to rename a file and I am getting erros and not sure why.

Here is my code

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main(){

   string path1, path2;
   int result;

   char var[30] = {0}; // Variable that will hold the data
   time_t now = time(NULL);
   struct tm *tm_data = localtime(&now);
   strftime(var, 30, "%m%d%y%H%M%S", tm_data);
   string tmpfilename(var);

   char newname[] = "C:\\hlmkjrnl\\" + tmpfilename + ".cook";

   path1 = "C:\\windows\\cookies\\tomax@creditcard[1].txt";
   path2 = "C:\\windows\\cookies\\tomax@creditcard[2].txt";

   if (access(path1.c_str(), 0) == 0){
      char oldname[] = "C:\\windows\\cookies\\tomax@creditcard[1].txt";
   }else if (access(path2.c_str(), 0) == 0){
      char oldname[] = "C:\\windows\\cookies\\tomax@creditcard[2].txt";
   }
     

    result= rename( oldname , newname );
    if (result != 0 )
      perror( "Error renaming file" );
 
    return 0;
}

This code produces this error:

C:\cplus>bcc32 test1.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test1.cpp:
Error E2141 test1.cpp 18: Declaration syntax error in function main()
Error E2268 test1.cpp 23: Call to undefined function 'access' in function main()

Warning W8004 test1.cpp 25: 'oldname' is assigned a value that is never used in
function main()
Warning W8004 test1.cpp 27: 'oldname' is assigned a value that is never used in
function main()
Error E2451 test1.cpp 30: Undefined symbol 'oldname' in function main()
Error E2285 test1.cpp 30: Could not find a match for 'rename(undefined,char *)'
in function main()
Warning W8013 test1.cpp 31: Possible use of 'result' before definition in functi
on main()
Warning W8004 test1.cpp 35: 'newname' is assigned a value that is never used in
function main()
*** 4 errors in Compile ***


However if I hard code the paths into the code it works:
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main(){

    int result;
     
    char newname[] = "C:\\hlmkjrnl\\110104125822.cook";
    char oldname[] = "C:\\windows\\cookies\\tomax@creditcard[1].txt";
    result= rename( oldname , newname );
    if (result != 0 )
      perror( "Error renaming file" );
 
    return 0;
}

The second works fine.

Any suggestions

Thanks,

jmsloan

LVL 3
jmsloanAsked:
Who is Participating?
 
waysideCommented:
  char newname[] = "C:\\hlmkjrnl\\" + tmpfilename + ".cook";

This won't work. Try this:

  string newname =  "C:\\hlmkjrnl\\" + tmpfilename + ".cook";

and then

  result= rename( oldname , newname.c_str() );
 
The other error :

Error E2268 test1.cpp 23: Call to undefined function 'access' in function main()

is because there is no function called access defined in any of the include files you've included.

If Borland provides an access function you need to look up in the docs which include file to use.

These errors:

Error E2451 test1.cpp 30: Undefined symbol 'oldname' in function main()
Error E2285 test1.cpp 30: Could not find a match for 'rename(undefined,char *)'

are because you declare oldname inside a pair of {} and it goes out of scope. Try:

   char *oldname;
   if (access(path1.c_str(), 0) == 0){
      oldname = "C:\\windows\\cookies\\tomax@creditcard[1].txt";
   }else if (access(path2.c_str(), 0) == 0){
      oldname = "C:\\windows\\cookies\\tomax@creditcard[2].txt";
   }
 
0
 
waysideCommented:
For Microsoft's compiler, access() (actually, _access) is defined in io.h . If it is the same for Borland you would need to add

#include <io.h> at the top of your program.
0
 
rstaveleyCommented:
>    char newname[] = "C:\\hlmkjrnl\\" + tmpfilename + ".cook";

tmpfilename is a std::string.

Try:

   string newname = "C:\\hlmkjrnl\\" + tmpfilename + ".cook";

You'll need to make it c_str() for access...

   if (access(path1.c_str(), 0) == 0){
0
 
rstaveleyCommented:
Sorry I was way to slow then. wayside said it faster and better :-)
0
 
grg99Commented:
Also take care, in any multitasking environment what access() tells you was true a bit ago, may not be true by the time access() returns to you.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.