ofstream unable to access file no matter what

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
using namespace 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;
}


Last edited on
setting the entire folder with the project to non read-only


The project dir isn't the same as the executable dir which isn't the same as the working dir.

On line 6, give the complete path, including directory.
I tried with this line and it still dosent work

ofstream myfile ("C:\\Users\\klip\\Desktop\\writetest\\writetest\\example.txt");
Where is your .sln file located?
C:\Users\klip\Desktop\writetest

writetest\writest is has the .cpp file
Last edited on
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.

Suggestions?
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
Topic archived. No new replies allowed.