Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tImmaY
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

*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..

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.

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.

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

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.

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

![]() |
![]() |
![]() |

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