File help

This is a discussion on File help within the Windows Programming forums, part of the Platform Specific Boards category; Hi all. My goal here is to copy the currently used file. Then start the new file and delete the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    File help

    Hi all. My goal here is to copy the currently used file. Then start the new file and delete the old one. An update sequence if you will.

    My code:

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	char *file="program.exe";
    	char *currentfile = argv[0];
    	CopyFile(currentfile, file, FALSE);
    	
    	if(file == currentfile){ 
    		Sleep(8000);
    		DeleteFile(argv[1]);
    	}
    	else{
    		ShellExecute(0,"open", file, argv[0], 0, SW_SHOW);
    		exit(1);
    	}
    	return 0;
    }
    Im stumped trying to figure this out. Any suggestions will be accepted (as long as their helpful =P). I think its trying to delete itself, which is 'not' what im trying to do. So if anyone can help me figure out what im doing wrong i'd be very greatful. Thanx in advance!
    Last edited by dellthinker; 11-18-2007 at 03:09 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Comparing strings like this
    Code:
    file == currentfile
    when the strings are C strings will not work - a constant and an argv[] string will NEVER have the same address, and that's what you are comparing.

    If that doesn't help, try some printf's to indicate what's in each of your strings, and follow what happens.

    Also, check the status of CopyFile() and ShellExecute()- If this fails, you probably want to "do something about it" - not sure exactly what, but you certainly shouldn't exit the current app if ShellExecute fails!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21