Simple Delete File

lil_bit 0 Tallied Votes 207 Views Share

Hi. Here is a simple beginners program. It uses "if_else" and returns no information. After execution, it deletes files within the same directory as the exe. Enjoy.

/* By lil_bit 2012: Free Open Source Code for all*/
/* remove example: This deletes file(s) from location the .exe is located.*/
/* Also notice only two headers with only one antiquated.*/
/* Please use care when using code*/
/* Namesta*/

#include <iostream>
#include <stdio.h>

int main ()
{
    if( remove( "testd1.txt" ) != 0 )
        perror( "Error deleting file" );
    else
        puts( "File successfully deleted" );

    if( remove( "testd2.txt" ) != 0 )
        perror( "Error deleting file" );
    else
        puts( "File successfully deleted" );

    if( remove( "testd3.txt" ) != 0 )
        perror( "Error deleting file" );
    else
        puts( "File successfully deleted" );

    return 0;
}
TrustyTony 888 pyMod Team Colleague Featured Poster

Now just to study about for loop.

lil_bit 0 Newbie Poster

Hello pyTony. ? Are you saying next progressional step; Better way to simplify code; Suggestion towards next article ? Please clarify. Thank you.

ravenous 266 Posting Pro in Training

Er, this looks very similar to the this example on cplusplus.com

lil_bit 0 Newbie Poster

Hello Ravenous! Your posts are very good. Yes it does. Thank you for bringing this to my attention. C++ is very modular so redundancy of similiar code on the net is not uncommon and thank you for the link and would you please grade or elaborate on this code? Thank you.

triumphost 120 Posting Whiz

DeleteFile("Meh.txt");

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I'd also study about useful output messages:
Error deleting file -- which file?
File successfully deleted -- which file?

lil_bit 0 Newbie Poster

Ravenous, you were right. Looking back through my study folder, I found my notes that cited the link. All I did was repeat the code. Credit to the original owner. Thanks to all for the feedback.

lil_bit 0 Newbie Poster

Click Here <<< once again, here is the link. Thank you.

lil_bit 0 Newbie Poster

Taking the advice of my peers. Here is a further edit of the code with proper citation. This code pauses to state files are either successfully deleted or displays error message.

/* remove example: This deletes file from location the .exe is located. */
/*Based upon original code found at http://www.cplusplus.com/reference/clibrary/cstdio/remove/ */
/*Code Modify By: lil_bit 2012 */

#include <iostream>
#include <stdio.h>

int main ()
{
  if( remove( "testd1.txt" ) != 0 )
    perror( "Error deleting file testd1.txt" );
  else
    puts( "testd1.txt successfully deleted" );

if( remove( "testd2.txt" ) != 0 )
    perror( "Error deleting file testd2.txt" );
  else
    puts( "testd2.txt successfully deleted" );

if( remove( "testd3.txt" ) != 0 )
    perror( "Error deleting file testd3.txt" );
  else
    puts( "testd3.txt successfully deleted" );

system( "PAUSE" );
return 0;

}
TrustyTony 888 pyMod Team Colleague Featured Poster

With my not so great C, I meant:

#include <stdio.h>

int main ()
{
    char * fn[] = {"testd1.txt", "testd2.txt", "testd3.txt"};
    int len = sizeof(fn) / sizeof(char*);
    for(int ind=0; ind < len; ind++)
    {
        if(remove(fn[ind]))
        {
            printf("Error deleting file ");
            perror(fn[ind]);
        }
        else
            printf("%s successfully deleted\n", fn[ind]);
    }
    return 0;

}
lil_bit 0 Newbie Poster

Thanks pyTony. Looks alright to me. I'm new to all of this, but, I make the best attempt. Wow, poweroutage and weird hack on me all at the same time. Considering your posts, you know what you are doing. Thank you pyTony. If anything, I am ready to learn.

mrnutty 761 Senior Poster

Since you are using C++ maybe avoid using C stuff.

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct RemoveFile{
   bool operator(const std::string& fileToRemove)const{
       if( std::remove( fileToRemove.c_str() ) ){
           std::cout << "removed: " << fileToRemove << endl;
           return true;
       }else{
           std::cout << "failed to remove: " << fileToRemove << endl;
           return false;
       }
   }
};

int main(){
    const std::string files[] = {"f1.txt" , "t2.txt" };
    std::vector<std::string> filesToRemove(files, files + sizeof(files)/sizeof(files[0]));       
    std::for_each(filesToRemove.begin(), filesToRemove.end(), RemoveFile());
    return 0;        
}

not really tested but give you an idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.