![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2005
Location: A House...
Posts: 191
Rep Power: 4
![]() |
Check if file exists, and delete file
Ok, I need to make a program that will firt, check if a files exists, and if it does, list it.
Then another part of the program needs to delete it. This is what I have for that. switch (c)
{
case '1' :
cout << "Scanning for files...";
remove("C:\\mydata.txt");
break;
}Is that the best way to delete a file? And how can i check if it exists? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
How 'bout something like:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("C:\\mydata.txt");
if (fin)
{
cout << "File found, deleting..." << endl;
system("DEL /F C:\\mydata.txt");
fin.close();
}
else
{
cout << "Could not find file!" << endl;
}
cin.get();
return 0;
}Will that work? As to remove(), I have only used it some, but I like DeleteFile(); better. System(DEL "myfile.txt"); works if you have Windows, but is not portable.
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2005
Location: A House...
Posts: 191
Rep Power: 4
![]() |
Thanks
. |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
I think remove() comes in cstdio, in which case you could just use it for the deletion. It is probably the easiest way to delete the file. To see if a file exists, you can try to create an fstream to it using the ios::nocreate flag, and if the process fails the file does not exist. However, putting these two together is mixing C and C++ file I/O. You can use FILE* fopen(const char*, const char*) with the file name and "r" instead. This is the C way of opening a file, and if the function returns a null pointer, then the file didnt exist (or you may have permissions errors).
|
|
|
|
|
|
#5 |
|
Professional Programmer
|
Hey tayspen,
I mistyped something in that code sample: if (fin)
{
cout << "File found, deleting..." << endl;
system("DEL /F C:\\mydata.txt");
fin.close();
}if (fin)
{
cout << "File found, deleting..." << endl;
fin.close();
system("DEL /F C:\\mydata.txt");
}Sorry about that.
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
Quote:
Thanks, I figured it out ![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|