Why am I getting a Permission denied error when compiling my c++ code?

  • Context: C/C++ 
  • Thread starter Thread starter chemistry1
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a user experiencing a "Permission denied" error when attempting to compile and run a C++ program. The conversation explores potential causes and solutions related to the compilation environment, code structure, and file permissions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes their C++ code and the issue of the console not executing the code, leading to a permission denied error when trying to access the output file.
  • Another suggests using getline(cin, nom); instead of cin >> nom; for input handling.
  • A participant inquires about the operating system and suggests running the IDE as an administrator to resolve permission issues.
  • Another participant proposes checking if the executable is still running in the task manager, suggesting that the name "test.exe" might be problematic.
  • One participant advises checking the properties of the output directory to ensure proper permissions are set for the user.
  • There is a discussion about whether the program is executing but not displaying output, indicating a potential misunderstanding of the program's behavior.
  • A participant mentions that the issue persists unless they delete and recreate the project file, raising questions about the initialization of the string variable.
  • Another participant points out that declaring the string variable without initialization is acceptable and that including #include is necessary for using the string type.
  • One participant shares that enabling C++11 support in the compiler settings resolved their issue, expressing confusion about how a syntax problem could lead to such errors.

Areas of Agreement / Disagreement

Participants offer various suggestions and potential solutions, but there is no consensus on a single cause for the issue. Multiple competing views on how to address the problem remain throughout the discussion.

Contextual Notes

There are unresolved questions regarding the specific permissions set on the output directory and the behavior of the IDE when compiling and running the code. Additionally, the impact of the C++ version settings on the compilation process is noted but not fully explored.

chemistry1
Messages
108
Reaction score
0
Hi, I'm having a serious trouble when I compile my code :

Code:
#include <iostream>

using namespace std;

int main()
{
string nom("x");
cout << "Vous avez quel age ?";
cin >> nom;
cout << "Vous avez-vous donc" << nom << "ans?" ;
return 0 ;
}

Here's the resume ; I click on compile&run and my console do open, but it doesn't execute the code.(nothing happens.) I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied. And that's not all, because the .exe has totally disapeared. So, basically, I can't run anything and need to delete my file and recreate a new one. The moment I add this code, the same thing happens. I have no idea what to do. (By the way, I just began learning.)

thanks !
 
Technology news on Phys.org
Try

Code:
    string nom;
    cout << "Vous avez quel age ?";
    getline(cin, nom);
 
chemistry1 said:
Hi, I'm having a serious trouble when I compile my code :

Code:
#include <iostream>

using namespace std;

int main()
{
string nom("x");
cout << "Vous avez quel age ?";
cin >> nom;
cout << "Vous avez-vous donc" << nom << "ans?" ;
return 0 ;
}

Here's the resume ; I click on compile&run and my console do open, but it doesn't execute the code.(nothing happens.) I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied. And that's not all, because the .exe has totally disapeared. So, basically, I can't run anything and need to delete my file and recreate a new one. The moment I add this code, the same thing happens. I have no idea what to do. (By the way, I just began learning.)
What operating system are you running? From what your wrote, I'm guessing you might be using Visual Studio on Windows. If you're getting a permission denied error, try starting Visual Studio as an administrator.
 
Im using windows and code block. And I am the administrator
 
After the program locks up, try running task manager, go to process and see if test.exe is still running. Not likely, but could the issue be the name? Try something other than "test.exe".

I assume that bin\debug\... directory exists?

You could try visual c++ express (it's free) to see if you have the same problem.
 
If bin\debug directory exists, check the properties of this directory to see if you as an admin have permissions (particularly execute permission) there. If not, you can add yourself. That should take care of the permission problem.
 
chemistry1 said:
it doesn't execute the code.(nothing happens.)

"Doesn't execute" doesn't follow form "nothing happens". Can be program does execute, just didn't reach the code showing anything on the screen.

I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied.

That could mean old process is still running, file is locked and it is not possible to replace it with a new copy of freshly compiled exe.
 
What would be the suggestion ?
 
You already got several, have you tried any of them?

Have you checked if something simple, like

Code:
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world!";
   return 0 ;
}

works?
 
  • #10
Well, yes it does work, but only if I do not put my code in before. If I do, forget it, nothing works anymore and I need to delete my file and create a new one so things begin working again. (My .exe goes somewhere else.)

Also, I think string nom("x"); might be a problem. I've should have putten char('x') since it's only one character.
 
  • #11
You were told to try to declare it as just
Code:
string nom;

You don't need to init it with a value (be it string "x" or a single character 'x').
 
  • #12
Ok good, I created a new project and did what you asked. It works. ( It didn't in my non working file)

Could you explain me what happened exactly ? ... I'm in the learning phase, so I don't know much. WHy did my things stop working with what I wrote ? thanks
 
  • #13
Don't you have to use also:

Code:
#include <string>

?
 
  • #14
Well, I don't know, haven't seen it. I barelly begun, it's been 2 weeks xD What does it exactly do ?
 
  • #15
Try this

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string nom;
    cout << "Vous avez quel age ? ";
    getline(cin, nom);
    cout << "Vous avez-vous donc " << nom << " ans?" << endl ;
    return 0 ;
}
 
  • #16
Oh, it looks like my problem has stopped. Someone has suggested me that my c++11 was maybe disabled. He told me to go to the compiler settings and put it to std=c++11 and guess what, it worked ! But still, I find it weird that a syntax problem could cause all this mess...
 
  • #17
chemistry1 said:
What does it exactly do ?

'#include <string>' gives you access to the 'string' data type and its associated operations, like '#include <iostream>' gives you access to 'count', 'getline", the '>>' operator for stream output, etc.

I'd expect that trying to declare a 'string' variable without first doing #include <string>' would give you a compiler error message. Maybe this has changed in more recent versions of C++ than the ones I've used.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
7
Views
3K
  • · Replies 70 ·
3
Replies
70
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K