Thread: File help

  1. #1
    Registered User
    Join Date
    Nov 2007

    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:

    #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){ 
    		ShellExecute(0,"open", file, argv[0], 0, SW_SHOW);
    	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 04:09 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Comparing strings like this
    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!

    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. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM