Determining if an inputted file name exists?
-
Thanks for looking at my question! I am trying to figure out how to determine if a user-inputted file name exists, and I am completely unsure how to do this in .net C++. I have tried just using the C fopen code:
if (fopen(String::Concat(room, ".txt"), "r") == 0)
(room is the inputted variable name, and .txt is obviously the file extension) But I get a "cannot convert System::String ^ to const char *" error. I am assuming this is because fopen requires a constant name like "Bedroom.txt" and wont accept a variable as part of the name, so I need help determining another way to see if a file exists. Any help would be greatly appreciated. Thanks!
-
Thanks for looking at my question! I am trying to figure out how to determine if a user-inputted file name exists, and I am completely unsure how to do this in .net C++. I have tried just using the C fopen code:
if (fopen(String::Concat(room, ".txt"), "r") == 0)
(room is the inputted variable name, and .txt is obviously the file extension) But I get a "cannot convert System::String ^ to const char *" error. I am assuming this is because fopen requires a constant name like "Bedroom.txt" and wont accept a variable as part of the name, so I need help determining another way to see if a file exists. Any help would be greatly appreciated. Thanks!
-
Try Google.[^] For this method, I believe you'll need the full path. I would also recommend reading up on the System.IO namespace. Dybs
Lol thanks! I actually just did this right before you linked me. But um, since I'm here. You wouldn't happen to know how to get the string value of a selected item in a list box would you?
-
Lol thanks! I actually just did this right before you linked me. But um, since I'm here. You wouldn't happen to know how to get the string value of a selected item in a list box would you?
-
Hmm I must be doing something wrong with a different part of my code then. I am trying to delete a file (in the same directory as the application) with a name that is respective to whats in a list box. For example there is a "doom.txt" file and the word "doom" in my list box. I select doom and hit the delete button and the "doom.txt" file should disappear. (what I'm doing is actually more complicated, but this is the part I'm having trouble with) Here is what my code looks like:
if (roomList->SelectedIndex != -1 && delCheck->Checked) { roomList->Items->RemoveAt(roomList->SelectedItem); File::Delete(String::Concat(Convert::ToString(roomList->SelectedValue), ".txt")); }
Any idea what I did wrong? Thanks for all the help :)
-
Hmm I must be doing something wrong with a different part of my code then. I am trying to delete a file (in the same directory as the application) with a name that is respective to whats in a list box. For example there is a "doom.txt" file and the word "doom" in my list box. I select doom and hit the delete button and the "doom.txt" file should disappear. (what I'm doing is actually more complicated, but this is the part I'm having trouble with) Here is what my code looks like:
if (roomList->SelectedIndex != -1 && delCheck->Checked) { roomList->Items->RemoveAt(roomList->SelectedItem); File::Delete(String::Concat(Convert::ToString(roomList->SelectedValue), ".txt")); }
Any idea what I did wrong? Thanks for all the help :)
you're removing the selected item from the list box. Then the next line you're trying to get the selected value (which will be null because you removed it already!). String::Concat will then create a string as ".txt". Now you're trying to delete a file called ".txt" which probably doesn't exist. It probably seems like nothing is happening because from MSDN[^] it says: Deletes the specified file. An exception is not thrown if the specified file does not exist. You could do something like this:
if (roomList->SelectedIndex > -1 && delCheck->Checked) { int nIndex = roomList->SelectedIndex; String ^sFileName = (String^)roomList->Items\[nIndex\]; if (!File::Exists(sFileName)) throw gcnew Exception("File does not exist!"); File::Delete(sFileName); roomList->Items->RemoveAt(nIndex); }
Don't be overcome by evil, but overcome evil with good
-
you're removing the selected item from the list box. Then the next line you're trying to get the selected value (which will be null because you removed it already!). String::Concat will then create a string as ".txt". Now you're trying to delete a file called ".txt" which probably doesn't exist. It probably seems like nothing is happening because from MSDN[^] it says: Deletes the specified file. An exception is not thrown if the specified file does not exist. You could do something like this:
if (roomList->SelectedIndex > -1 && delCheck->Checked) { int nIndex = roomList->SelectedIndex; String ^sFileName = (String^)roomList->Items\[nIndex\]; if (!File::Exists(sFileName)) throw gcnew Exception("File does not exist!"); File::Delete(sFileName); roomList->Items->RemoveAt(nIndex); }
Don't be overcome by evil, but overcome evil with good
Aha I see the logic in this now. It's funny how something so fundamentally obvious can completely escape my thought process haha. Anyway, thank you very much :D It appears to work now :D:D