I am developing a very simple notepad-type program in C++. The only problem is that when I prompt the user to delete their newly written text file, it doesn't work. Since I let the user pick the name for his/her file, I can't figure out the code to delete it. IT'S VERY URGENT. PLEASE HELP!!!

If (fSave == "n")
[
//How do I delete a file with a string name???
]

P.S.
To let the user choose the file name, I did
myfile.open (fname.c_str());

Dani AI

Generated

Short answer: make sure the stream that created the file is closed, then delete the file using a method that reports errors. and were right to point out closing the file first; the rest is about using the right API and checking why deletion failed (wrong working directory, permissions, or the file still in use).

A robust, modern approach (C++17 and later) uses <filesystem>. It accepts std::string/filepath values directly and gives a non-throwing overload that returns a useful error message:

#include <filesystem>
#include <iostream>
#include <system_error>

std::string fname = /* user input */;
std::error_code ec;
bool removed = std::filesystem::remove(std::filesystem::path(fname), ec);
if (removed) std::cout << "Deleted\n";
else std::cerr << "Could not delete '" << fname << "': " << ec.message() << '\n';

Troubleshooting checklist:

  • Ensure the ofstream/ofstream-like object that created the file is flushed and closed before deleting.
  • Use an absolute path or verify the process current working directory; a filename alone may not point to the file you think.
  • Check permissions and whether another process holds the file (Windows will refuse deletion if a handle is open).
  • Inspect the returned error (or call GetLastError on Windows) to get an authoritative reason.
  • Trim accidental whitespace from user input and confirm the expected extension is present.

If you cannot use C++17, use your platform API (Windows: DeleteFileW + GetLastError; POSIX: unlink) and check return codes. Mentioning specifically: if you opened the file with a name from the user, pass the exact same path when deleting and handle errors instead of assuming success.

Recommended Answers

All 5 Replies

Post your code, it will help us to identify your problem and answer is correctly.

P.S

You can always use method to delete a file.

And if its a windows API then

BOOL DeleteFile&#40;

LPCTSTR lpFileName   // pointer to file name

&#41;;

Could you please show me how to do the remove() function with a string fname?

First dont forget to close it
myfile.close()
if im not mistaken.

and then as np complete said
using the remove function posted in the msg

well since Remove() needs to get a char
just using the fname.c_str() should do the trick
remove(fname.c_str());

keep us posted

Thank you so much for the help :)

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.