File permission
I've created, and written to a file. It lets me do all of that just fine, but when I try to delete the file it says that I do not have permission. Is there something wrong with my code?:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char fileName[80] = "C:\\main.cpp";
ofstream fout(fileName, ios::app);
fout << "At the end\n";
fout.close();
ifstream fin(fileName);
char ch;
while(fin.get(ch))
{
cout << ch;
}
if (remove(fileName) == -1)
{
perror("Error deleting file: ");
}
else
{
cout << "Successful";
}
system("PAUSE");
return(0);
}
I have checked to make sure the file isn't read only, which it's not.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
you must close the file before it can be deleted. use fin.close() before attempting to delete it.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I've already tried that, it still gives me the error.
This also did not work:
system("del C:\main.cpp");
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Is file you want delete still on your editor?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
16th Oct 2005 was the last post before moorthi showed up.
I'm sure the OP found the answer in the meantime.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953