MoveFile function failure

Hi,
In my win32 application i want to rename some file.But MoveFile function is not working properly.code is given below.Please correct me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Rename(LPCTSTR Oldname,LPCTSTR Newname)
{
       FILE * FH;
       TCHAR File1[]={"abc.#sy"};//Present in the directory
       TCHAR File2[]={"bcd.#sy"};//Not in the directory
       TCHAR CurrntPath[]={"C:\PGM\abc.#sy"};
       TCHAR NewPath[]={"C:\PGM\bcd.#sy"};
       if(MoveFile(Oldname,Newname))//Working fine
       {
          errno_t err=_wfopen_s(&FH,File1,_T("r"));//Opening Properly
          if(!err)
          {
             if(MoveFile((LPCTSTR)CurrntPath,(LPCTSTR)NewPath))//Not working
             {
                   //Do some operation
              }
              else//Control going here
                 //movefile fails
          }       
       }
}
closed account (Dy7SLyTq)
i would just write your own. it shouldnt be that hard
closed account (N36fSL3A)
Why not just use a vector to get your stuffz, make a new file, copy the data over, then delete the previous file? Not that hard.
closed account (z05DSL3A)
If the function fails get extended error information with GetLastError. When you know what the error is you will be able to fix it or avoid it.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx
As C++ uses the backslash as the escape character, if you want a literal backslash in a string you need to use a pair of then, i.e. \\. Like...

TCHAR CurrntPath[]={"C:\\PGM\\abc.#sy"}; // which ends up as C:\PGM\abc.#sy after compiling

Andy

PS C++11 does also support raw string literals, which treat \ as \, but Visual C++ only supports it from Visual C++ 2013 (currently in preview). If you're using GCC, it's been available from version 4.5.
Last edited on
closed account (z05DSL3A)
andywestken, you give things up to easily. :0)
@andywestken: You are assigning a char string to a TCHAR string. ;P

EDIT: OP is doing the same thing.

http://www.cplusplus.com/forum/windows/106683/
Last edited on
@andywestken: Oldnamethis value is alzo not having"\\",but that one working fine.So only i did like that for .#sy file..I tryed for getting error information & GetLastError()returns a value 32.What it means?Please reply & help me.
Eror code 32 means ERROR_SHARING_VIOLATION, which is normal in this case, as you already used fopen to open the file, but without closing it.

The process cannot access the file because it is being used by another process.

A description of general error codes can be found here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx


There is also ErrLookup tool which came with the SDK or you can use FormatMessage() to get a string represation of the error code.
Thanks alot........Its working now
Topic archived. No new replies allowed.