Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 14th, 2006, 8:59 PM   #1
tayspen
Hobbyist Programmer
 
Join Date: Sep 2005
Location: A House...
Posts: 191
Rep Power: 4 tayspen is on a distinguished road
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?
tayspen is offline   Reply With Quote
Old May 14th, 2006, 9:24 PM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
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
Prm753 is offline   Reply With Quote
Old May 14th, 2006, 9:34 PM   #3
tayspen
Hobbyist Programmer
 
Join Date: Sep 2005
Location: A House...
Posts: 191
Rep Power: 4 tayspen is on a distinguished road
Thanks .
tayspen is offline   Reply With Quote
Old May 14th, 2006, 9:47 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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).
Jimbo is offline   Reply With Quote
Old May 14th, 2006, 10:38 PM   #5
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Hey tayspen,

I mistyped something in that code sample:
if (fin)
{
     cout << "File found, deleting..." << endl;
     system("DEL /F C:\\mydata.txt");
     fin.close();
}
should be
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
Prm753 is offline   Reply With Quote
Old May 15th, 2006, 5:00 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Jimbo
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.
I'm sorry to tell you, but ios::nocreate hasn't made it to the C++ standard.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 15th, 2006, 4:47 PM   #7
me22irc
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 me22irc is on a distinguished road
Quote:
Originally Posted by Prm753
Hey tayspen,

I mistyped something in that code sample:
if (fin)
{
     cout << "File found, deleting..." << endl;
     system("DEL /F C:\\mydata.txt");
     fin.close();
}
should be
if (fin)
{
       cout << "File found, deleting..." << endl;
       fin.close();
       system("DEL /F C:\\mydata.txt");
}

Sorry about that.

Thanks, I figured it out
me22irc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC