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

Detecting matching files?

P: 25
Hello, I need help with writing a part of a program which I am working on, I was wondering how to check for a pre-existing file. The user's input in the program is what determines the name of the file written in the end, but I would like to know how to make the program check to see if a file matching that name already exists, and then alert the user. Also I was wondering how to delete a file if the name is a string and not a constant using, (I've just been using system("del filenamehere.file") but I can't make it work for a string).
May 24 '07 #1
Share this Question
Share on Google+
5 Replies

P: 208
What have you got so far?
May 24 '07 #2

P: 79
So the user types in the filename? Is the filename used so that it gets created and data gets printed to it?
May 24 '07 #3

P: 25
The user types into a field their name, and from the first letter of their firstname and the last letter of their last name + their lastname it creates a textfile with this name. The part I need help with is how to detect if a file with a name matching the one the user is making exists already. So if a user named John mike Jacob entered this into the field and it created the file JmJacob, and then Jason mellon Jacob entered his name into the field, it wouldn't overwrite JmJacob with his information. I hope I explained what I'm asking a little more clearly.
May 24 '07 #4

Expert Mod 5K+
P: 9,197
Just open the file. If the file is not there, it's safe to create it. Like this:

Expand|Select|Wrap|Line Numbers
  1. ifstream file;
  3. if (
  4. {
  5.     cout << "No such file ao I  will create it." << endl;
  6."C:\\scratch\\aintnosuchfile.txt", ios::app);
  7. }
  8. else
  9. {
  10.   cout << "File exists already" << endl;
  11. }
May 25 '07 #5

P: 25
Thank you for the help
May 25 '07 #6

Post your reply

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