CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Copy files

  1. #1
    Join Date
    Dec 2003
    Posts
    99

    Copy files

    Hi,
    I am writing a C console application in .NET 2003 IDE.
    Is there any function to copy file(s)
    I have included "direct.h" header file but it does not seem to support copying file.
    I am left with the only system() call option which i guess does not satisfy me performance needs

    Thanks in advance

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Is the target system still the Windows environment? In this case, you can use the API functions 'CopyFile()'/'CopyFileEx()'...

  4. #4
    Join Date
    Dec 2003
    Posts
    99
    Thanks.
    Windows should be fine.

    Does the moving of the thread imply no non-visual C++ support to copy files ?

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...yes and no. The movement was done due to the fact that you are using Visual C++ .NET.

    However, there is indeed no standard function available...if you want to do it platform-independent you would have to implement it on your own...

  6. #6
    Join Date
    Mar 2004
    Posts
    31
    write the function it's easy!!

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Copy files

    Originally posted by nsh123
    Hi,
    I am writing a C console application in .NET 2003 IDE.
    Is there any function to copy file(s)
    I have included "direct.h" header file but it does not seem to support copying file.
    I am left with the only system() call option which i guess does not satisfy me performance needs

    Thanks in advance
    Then implement it yourself. Use fopen(), fread(), fwrite(), fclose(). Those are the C file I/O functions, and they are contained in <stdio.h>.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2003
    Posts
    99
    Originally posted by Loosejeans
    write the function it's easy!!
    I know its easy.
    I wanted to make sure i do not reinvent the wheel. Especially when it not going to do any good in performance.

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Originally posted by nsh123
    I know its easy.
    No, it certainly is not easy!
    It is even rather difficult to copy a file correctly on NTFS.
    Some points of difficulty:
    • Copying the security attributes of the file.
    • Encrypting destination file if source file is encrypted. But which encryption keys should be used: the keys used to encrypt the source file or the default keys of the user? What if the source file is encrypted, but the destination folder has encryption disabled?
    • Compressing destination file if source file is compressed.
    • Copying alternate streams of the file to the destination file.
    • What about sparse files? If you just use fopen/fwrite..., you will expand the sparse file and destination file will not be sparse anymore.

    Best solution to copy files on NTFS is SHFileOperation.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Marc G
    Best solution to copy files on NTFS is SHFileOperation.
    Which is rather a subjective opinion I might add...

  11. #11
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Originally posted by Andreas Masur
    Which is rather a subjective opinion I might add...
    Ok ... you would use CopyFile(Ex)?
    If you do, all alternate streams in the file will silently be ignored. That can't be good, is it?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  12. #12
    Join Date
    Sep 2005
    Posts
    7

    Question Re: Copy files

    Quote Originally Posted by Marc G
    Ok ... you would use CopyFile(Ex)?
    If you do, all alternate streams in the file will silently be ignored. That can't be good, is it?
    Is that so? I have read an alternate streams howto
    http://www.flexhex.com/support/resou...-streams.phtml
    and it says CopyFile does copy alternate streams. Is CopyFile really reliable or must I implement it myself?

  13. #13
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Copy files

    You do know that this thread is more than a year old?

    Anyway, the saves way to copy files is to use SHFileOperation.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  14. #14
    Join Date
    Sep 2005
    Posts
    7

    Re: Copy files

    Quote Originally Posted by Marc G
    You do know that this thread is more than a year old?
    Yes I know. What's wrong with it? Alternate streams are still with us, aren't they?

    Quote Originally Posted by Marc G
    Anyway, the saves way to copy files is to use SHFileOperation.
    Well I don't think SHFileOperation is the universal choice. It is kind of too high level with all those confirmation dialogs and progress bars and whatsit.

    Just FYI - I decided to investigate myself after all, and found that CopyFile does copy alternate streams, and in general works exactly as that article descibed. So there is no need to fiddle with SHFileOperation, CopyFile is quite safe.

  15. #15
    Join Date
    May 2005
    Posts
    4,954

    Re: Copy files

    Quote Originally Posted by freim
    Well I don't think SHFileOperation is the universal choice. It is kind of too high level with all those confirmation dialogs and progress bars and whatsit.
    it can be run also in silent mode, if you will specify the correct params, and no dialogs will appear.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured