Thread: File control

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    File control

    So I'm having trouble doing this is a semi-standard fashion:

    I have a file encryptor that does something special when prompted to decrypt a file.

    You pass the encrypted file as an argument to the program. The program reads the original filetype from the file (which I stored there upon encoding), and creates a temporary in some folder somewhere, where a decoded copy of the file is made.

    Now, after calculating a checksum, the file is launched via ShellExecute(), or whatever function is its equivalent on the system for which I'm compiling. Then the program waits for the user to be done. It does this:
    Code:
    while(!std::ofstream(path.c_str(), std::ios::app)) my::wait(5000);
    //now the file is ready
    Surprisingly, this method works for most filetypes on my Windows Vista system.

    Once the user is done, I recalculate the checksum to see whether the user has made an changes. If they made changes, I reencrypt and overwrite the original encrypted file, then delete the temporary. If not, then I just delete the temporary.


    Now -- my problems -- it's not working very consistently. For some files (i.e. Quicktime types), my program is allowed access to the file while the video is playing, and my program deletes the program form under quicktime's nose. Crash.
    Other files work well, but when I go to recalculate my checksum, I get a crash (i.e. BMP).
    And finally, some files (i.e. MSWord2007) are never overwritten because Word saves any changes in a temp and then does not update the original (compatibility mode?) .

    So my question is, first: is there anything wrong with my concept? Can this be done is a nearly-standard manner?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I doubt there's any way to do it automatically. Some programs will probably just read the file into memory and then close the file handle, so what would stop your program from deleting it then?

    Why can't you just leave a prompt up for the user indefinitely that says "Hit enter when done" or whatever and then do the checksum, delete, etc.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes, that is the obvious solution. I just wanted to make it fancy. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could wait until the program that is using the file exits. The easiest way to do this would be to pick programs that run in the foreground. Or you could somehow determine the program's process id and wait until that process quits. I'm not sure how you'd do this under Windows, but I'm sure it could be done.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yeah, but it still probably wouldn't be foolproof (not to mention more difficult to make platform-independent). What about programs like visual studio that allow multiple documents? Maybe you open an encrypted cpp file, modify it, then close the tab in visual studio (but visual studio itself is still open)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 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