unable to open file using complete path in ubuntu with boost C++ - c++

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!

Related

"Unrecognized file" when I try to open it C++

I'm using Netbeans 8.2 and I am programming with C++.
I'm trying to read a file. I have put it in the correct project folder of sources and I have used #include <fstream> .
ifstream myfile ("figures.txt");
code...
myfile.close();
I have the file in the correct place. But this message pops up:
Unrecognized file
Then, my other part of code also gives me error, because never detects the file I'm talking about open.
if (myfile.is_open()){
code...
}else{
cout << "file not open" << endl;
}
Auto solved, the .txt must be in the project folder, not in nbproject folder. At least it has worked doing it this way. Any more reccomendations will be accepted!

vs2008,vs2015 cannot read files: weird behaviour

I'm having a very weird behaviour in Windows 10 enviroment using both VS2008 and VS2015 when I try to read a file using ifstream.
I'm using the following simple code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("C:\\Users\\dd\\Desktop\\test.txt");
if(ifs.is_open())
{
std::string line;
while(std::getline(ifs,line))
{
std::cout << line << "\n";
}
}
else
{
std::cout << std::strerror(errno) << "\n";
}
return 0;
}
Obviously the file exists.
When I try to read the file .txt I get:
Permission denied
I run Visual Studio as Administrator and I have the permission to read the file because I am able to open it using Notepad++.
If I change the file extension, for instance .test, I am able to read the file content correctly and everything works as expected.
I uninstalled and then reinstalled VS2008, VS2015 and all C++ Redistributable, but nothing changed. Any help is greatly appreciated!
Perhaps your file isn't named test.txt. Did you check that your file is really named test.txt and not test.txt.txt (with a hidden extension)?
Try to list files in command prompt to see what the real file name is:
cmd>
cd C:\Users\dd\Desktop\
dir | findstr test
Onother possibilities:
test.txt could possibly use similarly looking letters from another
language (in your code or in actual file name).
not properly quoted slashes in your code (e.g. C:\Users\\ instead of C:\\Users\\dd\\). Try to change to forward slashes.

Failing File I/O is_open() in Qt Creator

I'm using QtCreator 3.6.0 - Based on Qt 5.5.1 (Clang 6.1 (Apple), 64 bit).
I'm trying to read a text file but I keep on getting this error message,
Unable to open file/Users/... ...
Here is the code extract,
string line;
ifstream myfile ("Config.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
My 'Config.txt' file is in the same project folder where main.cpp is.
Where am I going wrong?
For QtCreator, you should change the working directory to the directory containing Config.txt. You can find the "Working directory" setting for your executable via the sidebar "Projects" => "Build & Run" => "Run"
See: https://doc.qt.io/qtcreator/creator-run-settings.html

I can't read a .dat file using fstream

I know there are a ton of questions pertaining to this subject but I cannot get this to work. The program ran fine on my laptop, but when I try to compile and run it in the the schools Linux lab the program cannot open the file. I have tried defining the absolute file position but nothing has worked. The file name is correct and everything but when I try to run the program it displays "failed". I'm using gedit and compiled the program with bash.
ifstream fin("rainfall.dat"); // If the file cannot open display failed
if(fin.fail()){
cout << "failed" << endl;
return 1;
}
try
#include <errno.h>
if(fin.fail())
perror("open failed ");
this will give you a human readable message for the last error

How to run a executable file in c++ in Linux?

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"