Computing.Net > Forums > Programming > C++ copy / delete file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ copy / delete file

Reply to Message Icon

Name: tImmaY
Date: July 14, 2004 at 09:26:24 Pacific
OS: Windows XP Pro SP1
CPU/Ram: AMD Athlon XP 2400+/512 m
Comment:

Hello, I'm wondering how I could copy and then delete a file in C++. What I want to do is copy a file, then delete the original so that i can encrypt it and name it the original file. then i want to delete the temp file. but the only things i need to know are how to copy and then delete a file, i have all of the encryption already. thanks! ::tim



Sponsored Link
Ads by Google

Response Number 1
Name: tImmaY
Date: July 14, 2004 at 09:51:33 Pacific
Reply:

*ahh..* the joys of batch commands in C++ programming..
also..*ahh*..the joys of stumbling across something after posting a question on a forum about it.

anyways, to copy its just:
system("copy shibby.txt temp.txt");

to delete:
system("del shibby.txt");

lol yea.. that was too easy..


0

Response Number 2
Name: Don Arnett
Date: July 14, 2004 at 10:38:57 Pacific
Reply:

It would be more efficient to:

- rename the original file
- read the renamed file and write encrypted file to original file name
- delete the renamed file


File I/O functions are specific to compilers, so you'll need to check your docs to see what file handling functions it has.

You should have a 'system()' function which allows you to run DOS-like commands. For example:

char cmdBuff[200];

sprintf(cmdBuff,"rename %s %s",origFileName,newFileName);
system(cmdBuff);

For deleting a file, look for an 'unlink()' function.


0

Response Number 3
Name: Don Arnett
Date: July 14, 2004 at 10:40:26 Pacific
Reply:

I started typing my answer, then was interrupted and then when to lunch. Meanwhile, you found the system command.

I'd still suggest that you should rename rather copy/delete.


0

Response Number 4
Name: tImmaY
Date: July 15, 2004 at 07:58:23 Pacific
Reply:

ic ic, lol. its all good.. :)

but aren't they essentially doing the same thing? only with renaming i'm creating a new file and then deleting the original instead of copying then deleting the original. i'm not sure which would be more efficient but i've gotten everything to work and i'm happy with it. now i just get to make things more complicated.. but you can look at it if you want. its either in a previous post i made (2 of mine ago) or you can download it from: http://www.freewebs.com/anarchy85/thelist.cpp


0

Response Number 5
Name: Don Arnett
Date: July 15, 2004 at 18:45:35 Pacific
Reply:


Renaming is not the same as creating a new file, copying the old file into the new file and deleting the old file.

In any file system, a file consists of two things (this is a simplified example).

First, there is the file, which is a bunch of bytes stored in a group on the harddrive.

Second, there is an entry in a table for that file. The entry contains info like the file location, creation date, file name, maybe it's size, etc.

Normally, when you rename, all that happens is the OS goes to that file's entry in the table and changes the name. The actual file doesn't get touched. Why move the file around when all you have to do is change the label.

Now, there are cases where a rename will cause a file to get copied and the original deleted. When the rename causes the file to move from one disk device to another.

I don't know if this is legal in Windows, but it'll make the point.

rename C:\myfileA.txt D:\myfileB.txt

Each drive keeps it's own table of file info. So changing the entry in the file table on drive C doesn't cause the file to be moved to D. So even tho all you told it to do was a rename, the OS will be smart enough to do a copy to the new name on the new drive and delete the old file on the old drive.

I don't know if windows will do that, but other operating systems will do that.


0

Related Posts

See More



Response Number 6
Name: tImmaY
Date: July 15, 2004 at 23:47:20 Pacific
Reply:

hmm.. ya make a good point kid. it would make more sense to just rename it.. i wonder if i should defrag soon because it seems that the way i've been doing it would create the new file in a bunch of locations on my hard drive just to delete them. so that new files that i put on my hard drive would be somewhere off in lala land. idk, i'm not a guru of any sort so i'm just talking out of my bum so i'll shut up now. :) thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ copy / delete file

Batch File deleting files by date? www.computing.net/answers/programming/batch-file-deleting-files-by-date/13717.html

Batch File deleting files by date? www.computing.net/answers/programming/batch-file-deleting-files-by-date/15581.html

Delete Files Older Then x-Days www.computing.net/answers/programming/delete-files-older-then-xdays/15229.html