My main code was working a few days ago. Today I tried to run it again and it refused to write no matter what I did, so I used the simplest code I could write for ofstream and it gave me the same error
Output file failed to open because: Permission denied
The simple code is below, can anyone give me a solution to this issue? I am using Visual c++ 2010 (yes its very old), I created an example.txt in
C:\Users\ \Desktop\writetest\writetest
I have tried
1. setting visual studio to adminstrator
2. setting the entire folder with the project to non read-only
3. restarting computer
4. Looking at task menu
5. Shifting the file from desktop to documents
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else {
std::cerr << "*** error: could not open output file\n" ;
perror("Output file failed to open because: ");
}
cin.ignore(2);
return 0;
}
I got back home (was doing this at work during break and stuff) and the exact same codes for this and my main program works perfectly fine on my desktop.
The file must be in the same folder as the solution file .sln if you run it inside VS.
You also could try to copy it to the Debug/Release folder and run the .exe from the command line.
I made duplicates and placed them in all folders but it didnt work either. It seems to be a problem from (I am purely guessing here) a windows update from last thursday the last time I ran this file was coincidentally right before the update