We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Simple Delete File

0
By lil_bit on Jul 17th, 2012 5:57 pm

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;
}

Now just to study about for loop.

pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

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

lil_bit
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 8

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.

lil_bit
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

DeleteFile("Meh.txt");

triumphost
Practically a Master Poster
625 posts since Oct 2009
Reputation Points: 59
Solved Threads: 55
Skill Endorsements: 1

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

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
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

lil_bit
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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;

}
lil_bit
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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;

}
pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

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.

lil_bit
Newbie Poster
22 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 14

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1125 seconds using 2.76MB