Solved

renaming a file based of a time

Posted on 2004-11-01
241 Views
Last Modified: 2010-04-02
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

0
Question by:jmsloan
    5 Comments
     
    LVL 14

    Accepted Solution

    by:
      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
     
    LVL 14

    Expert Comment

    by:wayside
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    >    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    Sorry I was way to slow then. wayside said it faster and better :-)
    0
     
    LVL 22

    Expert Comment

    by:grg99
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now