Failing File I/O is_open() in Qt Creator - c++

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

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!

{c++} programs send error 0xc00007b when run on a computer without visual studios proper dlls are in place

I have this program:
#include <iostream>
#include <string>
using namespace std;
int main() {
string inputfile = "input.pdf";
string outputfile = "output.tiff";
cout << "paste path of pdf to convert" << endl;
getline(cin, inputfile);
cout << "type the path of the output file with the correct extension ie png jpeg or tif" << endl;
getline(cin, outputfile);
string command = "gm.exe montage -tile 1x10000 -geometry 85% -density 150x150 " + inputfile + " -quality 100 " + outputfile;
system(command.c_str());
return 0;
}
As you can see its using the std library. I did not use a setup project because it felt unnecessary as A could easily just copy and paste the application and the dlls. Before I included dlls in the copy it gave me the following error message:
MSVCP140d.dll is missing and the program cant start.
I got a copy of the DLL from my computer and that seemed to solve the issue, but then when I ran it on the non development computer it gave me:
ucrtbassed.dll is missing and the program cant start.
I downloaded the missing DLL from the internet, but now when I go to launch the program I get this error:
The application was unable to start correctly (0xc0000007b}. Click OK to close the application.
It seems that when a computer has Visual Studio installed the code runs fine; however, when they don't have Visual Studio installed it doesn't run. I have installed the VCredist.exe and the same problem happens.
regards Shrimp
Build you project Release version. Not the Debug. Symbol d at the end of .dll name shows that you have built debug version.
Link all libraries statically right clink on Project -> Properties (-> select Release configuration and All Platforms at top of the window) -> C/C++ -> Code Generation -> set "Runtime Library" to "Multi-threaded (/MT)"
Don't forget to escape arguments you pass to command line for other app. I fixed one line of your code. See additional quotes around file names.
string command = (string)"gm.exe montage -tile 1x10000 -geometry 85% -density 150x150 " +
"\"" + inputfile + "\" -quality 100 \"" + outputfile + "\"";

C++ Project Won't Write to file After Export From xCode

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?

ifstream stopped working in eclipse

I have a C++ project that I haven't used in a year. It used to run perfectly, opening files with:
Matrix pmatrix;
pmatrix.readFromFile("pmtx.txt");
and in the function:
void Matrix::readFromFile(string filename){
ifstream tmfile;
tmfile.open(filename.c_str());
if (!tmfile) {
cout << "unable to open tm file";
//return 1;
}
etc
}
It keeps telling me "unable to open tm file". I've been breaking my head about this. Obviously the file is in the directory of the src and I copied it to Debug and Release, I gave it full permissions. I use eclipse.
Any ideas why this just stopped working?
There was something strange screwed up with the eclipse project. I created a new project, copied in existing files and it worked.

unable to open file using complete path in ubuntu with boost 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!