Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to delete and copy files?

DiggaDigga Posts: 32Member
I know how to use the delete file function, but how do I program in the path of the file? So for example, I have a file called xx.txt and it is located in C:desktopMy_crapMy_Junk; How to you delete it? You would have to specify the path. But I don't know how to do that. Also what is the function for copying files?

--Dave
«1

Comments

  • MT2002MT2002 Posts: 1,444Member
    : I know how to use the delete file function, but how do I program in the path of the file? So for example, I have a file called xx.txt and it is located in C:desktopMy_crapMy_Junk; How to you delete it? You would have to specify the path. But I don't know how to do that. Also what is the function for copying files?
    :
    : --Dave
    :
    [code]
    fclose (fopen ("c:desktop\My_crap\My_Junk\xx.txt", "r+"));
    [/code]
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-5-28 14:24:53[/red][/b][hr]
    : : I know how to use the delete file function, but how do I program in the path of the file? So for example, I have a file called xx.txt and it is located in C:desktopMy_crapMy_Junk; How to you delete it? You would have to specify the path. But I don't know how to do that. Also what is the function for copying files?
    : :
    : : --Dave
    : :
    : [code]
    : remove ("c:desktop\My_crap\My_Junk\xx.txt");
    : [/code]
    :


    there are several ways to copy a file. The easiest is to use the system() function.
    [code]
    char *source_file = "c:desktop\My_crap\My_Junk\xx.txt";
    char *destination_file = "c:desktop\My_crap\My_Junk\duplicate.txt";

    char command[255];
    sprintf(command,"copy %s %s",source_file,destination_file);
    system(command);
    [/code]



  • John SamuelsJohn Samuels Posts: 25Member

    using C++ iostream should be easiest:

    #include
    #include

    int main() {
    ifstream in("c:desktop\My_crap\My_Junk\xx.txt");
    ofstream out("c:desktop\My_crap\My_Junk\duplicate.txt");
    out<<in.rdbuf(); // copy file
    in.close();
    out.close();
    }
  • John SamuelsJohn Samuels Posts: 25Member
    :
    : using C++ iostream should be easiest:
    :
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main() {
    : ifstream in("c:desktop\My_crap\My_Junk\xx.txt");
    : ofstream out("c:desktop\My_crap\My_Junk\duplicate.txt");
    : out<<in.rdbuf(); // copy file
    : in.close();
    : out.close();
    : return 0;
    : }
    :

  • DiggaDigga Posts: 32Member
  • stoberstober Posts: 9,765Member ✭✭✭
    How does that work? first reads the whole file into memory then writes it out to the output file? Great for small files, but won't work on large ones if the file is larger than available memory.
  • John SamuelsJohn Samuels Posts: 25Member

    Good question haven't thought about that.
    I just used the example in the book ( Bruce Eckel's Thinking in C++ volume Two : Practical programming page 178 ) as a base.
    I suppose that with real big files virtual memory (hard disk based memory) is used. I suppose this should be a compiler/hardware issue and
    of no concern to the programmer.
    If I read the piece in Bruce Eckels's book about iostream buffering/
    streambuf/rdbuf, I cannot find anything about restrictions when using these items.
    But I am not really sure, I'm just guessing.

  • LundinLundin Posts: 3,711Member
    :
    : Good question haven't thought about that.
    : I just used the example in the book ( Bruce Eckel's Thinking in C++ volume Two : Practical programming page 178 ) as a base.
    : I suppose that with real big files virtual memory (hard disk based memory) is used. I suppose this should be a compiler/hardware issue and
    : of no concern to the programmer.
    : If I read the piece in Bruce Eckels's book about iostream buffering/
    : streambuf/rdbuf, I cannot find anything about restrictions when using these items.
    : But I am not really sure, I'm just guessing.
    :
    :


    The operative system has got specific functions for file copy, which are probably more effectivly implemented than the open/read file functions.
    If you use fopen() the compiler doesn't know what you are going to do with the file, so it can't choose the most effective function.

    The ANSI C standard has no way to copy a file effectivly, which could be because they talk about streams, not files. On the other hand that argument is stupid, because there are functions like remove(), rename(), tmpfile() etc. So it seems there is no logical explanation to why that function is missing.

    If you want to copy a file effectivly, you must fall back to the OS specific code, like system("copy file.txt");

    Or in Windows, preferably use the CopyFile() function.
  • siyuan123siyuan123 ChinaPosts: 1Member

    Using http://www.duplicatefilter.com is a good and easy way to find and delete duplicate files on hard drives.

  • sonhousonsonhouson USAPosts: 1Member
    edited January 7

    For various reasons, you may lost data on Mac, such as accidental deletion, computer crash, virus infection, improper formatting, and other operations.On the surface, you can no longer recover those lost files, actually, all of those lost data or formatted/deleted files are still on your Mac.However,you must to use a professional Data Recovery for Mac to recover deleted files in Mac. There are several utilities available to help recover deleted files under Mac OS X. Your chances of success depend on how the file was deleted or lost.

«1
Sign In or Register to comment.