NO

Author Topic: Should I stay away from the ReplaceFile function just use Remove + Rename?  (Read 4472 times)

EdPellesC99

  • Guest


   Hi,
   Here I have a code block that works fine in a simple console file (uses -ZE option of course).[Note it replaces a file1.txt to a file0.txt, but neither were ever operated upon (fopen, "w" or fclose etc) within the simple source file.

  However I have a more complicated file, that during the file opens and writes file0.txt and file1.txt several times before I want to use ReplaceFile to replace one with the other.

  In this source file, before I try to ReplaceFile, I close the file streams.
 
(I found a link: http://support.microsoft.com/kb/274487 that talks about the need for Write Access on file0.txt.)

Now in my complex source file instead of ReplaceFile I can use the remove() function on file0.txt then use the rename() function on file1.txt changing it to file0.txt no problem.

To me this proves I had file0.txt closed properly, or I could not remove it!

  Now in my more complex file, ReplaceFile works to a dummy file ( eg. not "file0.txt", but "dummy.txt" ) it works because my program never opened wrote and closed dummy.txt.

  Any opinions? If ReplaceFile is goofy, then I will stay away from it. But it seems powerfull, and usefull, and I would like to know if I am doing something wrong.

  Thanks,

   Ed


Quote

// // ~ •  •  •  •  •  •  •  •  •  •  •  •
char *lpFtogetNew_contents = "file0";
char *lpReplF_contents = "file1";
// // ~ Ednt: Once Done Contents of file0.txt is the former contents ot file1.txt which no longer exists.
LPVOID lpExclude;
LPVOID lpReserved;
// // ~ •  •  •  •  •  •  •  •  •  •  •  •
BOOL test = ReplaceFile(
               lpFtogetNew_contents,
               lpReplF_contents,
               0,
               //      "backup.txt", if used would have the orig contents of file0
               REPLACEFILE_IGNORE_MERGE_ERRORS,
               lpExclude,
               lpReserved
               );
// // ~ •  •  •  •  •  •  •  •  •  •  •  •
printf("test is %i", test);
getchar();

// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Seems to be some errors. The file names are not complete of extensions (but maybe you have simplified it).
The reserved pointers lpExclude and lpReserved are defined and left uninitialized, in this case you should use a NULL.
To replace a pointer, in our case the backup file name, you must use the null pointer (NULL) not 0 (zero).
Now try the following code and check the error at this page http://msdn.microsoft.com/en-us/library/aa365512(VS.85).aspx

Code: [Select]
char *lpFtogetNew_contents = "file0.txt";
char *lpReplF_contents = "file1.txt";
// // ~ Ednt: Once Done Contents of file0.txt is the former contents ot file1.txt which no longer exists.
// // ~ •  •  •  •  •  •  •  •  •  •  •  •
BOOL test = ReplaceFile(
               lpFtogetNew_contents,
               lpReplF_contents,
               NULL,
               //      "backup.txt", if used would have the orig contents of file0
               REPLACEFILE_WRITE_THROUGH | REPLACEFILE_IGNORE_MERGE_ERRORS,   //added this flag to assure that file buffer is flushed
               NULL,
               NULL
               );
// // ~ •  •  •  •  •  •  •  •  •  •  •  •

if (!test)    //test==0 -> error
    printf ("Error in function. Error code 0x%x\n", GetLastError());
else
    printf("No function errors...\n");

getchar();

« Last Edit: August 25, 2010, 10:21:57 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

EdPellesC99

  • Guest

    Frankie,


   That did it! Thank you very much !

  Yes, file0 was a char variable for file0.txt in my file.

  Ok, I made careful note of all that you said. Thank you for the detailed intstruction. It means a lot to me,
and I will remember you  as I use NULL instead of 0 (afraid I have been doing a lot of that in other functions too).

  Super, the ReplaceFile function is working in my more complex file just fine.

  I saw your response as I was about to post a source file example which proved there was more than the
open/write/close on the files file0.txt and file1.txt involved. I had made a simple source file which very simply
performed one i/o write for each file, and the ReplaceFile function worked ... I was then wondering if the problem
was using the function from the Switch case section of my "more complex file".

  Your input solved everything, and I would have continued to troubleshoot in the wrong direction, and probably
have given up on the ReplaceFile function.  I was confused by it working in a simple situation
then not where I wanted it to work.



  Again thanks, appreciate it Frankie,

...... Ed