Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

renaming a file based of a time

Posted on 2004-11-01
5
Medium Priority
?
268 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
Comment
Question by:jmsloan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
wayside earned 400 total points
ID: 12467495
  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
ID: 12467534
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
ID: 12467551
>    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
ID: 12467560
Sorry I was way to slow then. wayside said it faster and better :-)
0
 
LVL 22

Expert Comment

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

610 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