By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

remove a file with variable name

P: 1
hi
i have a problem in removing a file with a variable name.
i know the function remove("file.txt")
takes a string. what can i do to make it take a variable.
or is there another function to remove a file with a variable name.i take it as an input

thanks
Jun 17 '07 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
hi
i have a problem in removing a file with a variable name.
i know the function remove("file.txt")
takes a string. what can i do to make it take a variable.
or is there another function to remove a file with a variable name.i take it as an input

thanks
make a variable of char * type
e.g

char *filename;
remove(filename);

Have fun with it.

And welcome to the scripts

Savage
Jun 17 '07 #2

archonmagnus
100+
P: 113
hi
i have a problem in removing a file with a variable name.
i know the function remove("file.txt")
takes a string. what can i do to make it take a variable.
or is there another function to remove a file with a variable name.i take it as an input

thanks
You could also make use of the string class by using something on the order of:

Expand|Select|Wrap|Line Numbers
  1. string filename;
  2.  
  3. cout<<"File to delete: ";
  4. getline(cin, filename);
  5.  
  6. if (remove(filename.c_str()) == 0)
  7.     cout<<"Delete Successful!"<<endl;
  8. else
  9.     cout<<"Can't Delete!"<<endl;
This would take into account filenames with spaces as well.

FYI, OP: remove returns a 0 (zero) if successful...
Jun 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.