I\O Copy & Delete

This is a discussion on I\O Copy & Delete within the C++ Programming forums, part of the General Programming Boards category; thats say there are two files file abc.txt file def.txt and i wann delete the contents in abc.txt and copy ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    I\O Copy & Delete

    thats say there are two files

    file abc.txt

    file def.txt

    and i wann delete the contents in abc.txt and copy the contents in def.txt to abc.txt then delete the def.txt file

    any function to do that?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Delete - see if either foo.empty() or foo.clear() works, I don't have a compiler on this computer so I can't find out that answer for you

    Copy - Don't know of a function but you could try something like this
    Code:
    while (!infile.eof())
    {
         outfile << infile.get();
    }

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Try:
    Code:
    #include <cstdio>  //For rename()
    #include <windows.h>  //For DeleteFile() - Windows specific.
    
    ...
    DeleteFile("abc.txt");
    rename("def.txt", "abc.txt");
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Look up unlink(). It's in cstdlib I believe.
    Or, you could use remove() in cstdio.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Quote Originally Posted by Hunter2
    Try:
    Code:
    #include <cstdio>  //For rename()
    #include <windows.h>  //For DeleteFile() - Windows specific.
    
    ...
    DeleteFile("abc.txt");
    rename("def.txt", "abc.txt");
    i dont find there is rename for cstdio @@

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    In cstdio:
    Code:
    int remove(const char* filename);
    Removes specified file. Returns non-zero on failure.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    thx

    Quote Originally Posted by sean_mackrory
    Actually that's a windows function too.
    u mean like this?
    system ("del abc.txt");
    system ("ren def.txt abc.txt");

    however i am trying to find other way out

    but thank you all

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And actually I had to correct myself, I got myself mixed up. system() is a bad idea when there are more portable options. Have a look at http://www.cprogramming.com/tips/ to find out why, but until then - just use the functions suggested to you above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. MSVC 2003 Debugging delete
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 09-09-2007, 12:15 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. Delete Item
    By emilyh in forum Windows Programming
    Replies: 10
    Last Post: 10-03-2001, 09:33 PM

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