I am trying to run an executable file within a c++ code. It compiles and run, but it displays a message "permission denied". This same code works on windows, I just use the appropriate file path.
Here is the code I am using
FILE *fp = popen("/home/Int_Outputs/bin/Debug","r");
if (fp == NULL){
std::cout <<"Popen is null" << std::endl;
}
char buff[50];
fgets(buff,sizeof(buff),fp);
std::cout << buff;
}
return 0;
}
What are the POSIX permissions for that file?
To find out, open a terminal shell and do:
$ ls -l /home/Int_Outputs/bin/Debug
You have to make sure that the UID/GID that your application runs as has permission to read the file "/home/Int_Outputs/bin/Debug"
Related
I hope I am not duplicating posts from the past (although I have read and tried many existing posts in stackoverflow without any gain). I have a VC++ application where I am trying to call a function that would then run a Python file. I have Python 311 installed and configured via the project settings. The code compiles fine but I believe does not run the file encode_post.py. The returned value from PyRun_SimpleFile is 0 (if I run another file the result is -1). In the encode_post.py file, I am simply opening a test.txt file and appending the command line argument argv[1] "Hello world" (I tried without providing the argument too). If I double click encode_post.py in the folder or run it via the command prompt, it runs fine and writes the line in test.txt, but running the file via C++ does not append anything. Below is my code which does not throw any error or breaks the application. In project settings, I also configured to run Debug DLL in runtime library in code generation. Any advice would be great as at this point I am not sure where exactly the issue is arising. I also have #include <Python.h> in my code without any error
char* argv[2];
argv[0] = "encode_post.py";
argv[1] = "Hello world";
try {
Py_Initialize();
Py_SetProgramName((wchar_t*)argv[0]);
PySys_SetArgv(2, (wchar_t**)argv);
file = fopen(argv[0], "r");
if (file) {
int result = PyRun_SimpleFile(file, argv[0]);
fclose(file);
}
Py_Finalize();
}
catch (IOException^ ex) {
// Could not open the file
}
Use _Py_fopen_obj. The snippet below works for me.
PyObject* obj = Py_BuildValue("s", argv[0]);
FILE* file = _Py_fopen_obj(obj, "r");
if (file) {
PyRun_SimpleFile(file, argv[0]);
}
inputfile = fopen("input_file.txt", "r");
outputfile = fopen("output_file.txt", "w");
char a[30];
// fgets (a , 100 , inputfile);
fscanf(inputfile, "%s", a);
I am using QtCreator. In class constructor when i use that codes and run, i just see "Press RETURN to exit" in terminal. But when i compile my program with g++ and run, it works well.
Probably there is a problem with fscanf or fgets in QtCreator. Because if i dont include that functions my program works also.
The most likely problem is that you do not have a file called input_file.txt in the same directory as the directory that the binary is being run from. The binary directory is usually not the same as the source directory when running from an IDE.
I am having a strange issue. I have a function (writedata) that take a string and writes it to a file. Is works fine and files are created when I build and run the program within xCode.
When I export the compiled project from xCode and just try to run the executable terminal app no file is ever written. I can see the process completes in terminal with no errors given. I even get the 'cout<<"Done. File Written!"' message but no file.
void writedata(string writedata, string filename){
ofstream data(filename, ios::app);
if (data.is_open())
{
data<<writedata;
cout<<"Done. File Written!";
data.close();
}
else cout << "Unable to open file";
data.close();
}
Does anyone know why this is happening?
Playing around wit boost library using ubuntu, with the following code the file main.cpp gets open and being printed but when I try to open some other file on some other location, it does not work.
Here is the code
std::string line;
boost::filesystem::ifstream file ("main.cpp") ;
if (file.is_open())
{
while ( getline (file,line) )
{
std::cout << line << '\n';
}
file.close();
}
else std::cout << "Unable to open file"<< std::endl;
Here is the path that I am giving and its not working.
boost::filesystem::ifstream file ("/home/0circle/workspace/practice/main.cpp") ;
can someone tell me what could be the problem and how to cure it ?
Thanks :)
Make sure the file exists and you have permissions for reading the file:
execute in shell: ls -l /home/0circle/workspace/practice/main.cpp
verify if you have read rights (first column of the output of the
previous shell command). See this!
try to find out reason of failure: See this!
I have a c++ program which read and writes to a file. When i invoke it from bash shell such as ./modifyFile /path/to/file.abc.12.def, it works. But, if i do the same from a bash script such as show below:
#!/bin/bash
./modifyFile /path/to/file.abc.12.def
the c++ program is unable to open file. I print the filename, that is passed to the program and output is correct.
Infact, if i copy the filename which is printed by the c++ program and manually execute it on bash script such as ./modifyFile /path/to/file.abc.12.def, it works. So there are no user permissions issue either.
The c++ code, where file is not found is shown below
fstream aFile(filename.c_str(), fstream::ate | fstream::in | fstream::out);
if(!aFile)
{
cerr << "Unable to open file!" << endl; // this prints from bash script
return EXIT_FAILURE;
}