Can' open File in C++ under MacOs - c++

I want to open and read a file in C++. Therefor I wrote the following code:
#include <fstream>
#include <iostream>
#include <string>
...
string line;
ifstream file;
file.open("./db.config");
if (file.is_open()) {
cout << "File is open" << endl;
getline(file, line);
file.close();
}else cout << "File is not open" << endl;
This code is written in the main.cpp. I verified that main.cpp and db.config are in the same directory.
I don't get any Compiletime oder Runtime Errors. It only prints "File is not open". I also tried it without "./" ( file.open("db.config"); ), but this also didn't work.

The problem is, the current working directory is not the one where db.config file is located. You seem to have it in the same directory as the .cpp file. The current working directory is probably something different. Ultimately you need to decide where you want db.config file to reside, there are many options, but here's simple solution:
See where the application binary is.
Copy db.config there if it isn't there already.
In your code, change to that directory before you load the file, which you can do with Qt like this:
QDir::setCurrent(QCoreApplication::applicationDirPath());
Note that if the user runs the program from command line, and are allowed to give files as arguments, then changing working directory inside the program might make those files not be found. In that case, construct absolute path to db.config instead of changing the working directory.
You could read QStandardPaths docs to get better idea on where you actually want to store the db.config file. This depends on how you plan to distribute the application. If you just want to have it in .zip or something, then same directory with application binary is probably fine.

Related

Why is my fstream not creating data1.txt file?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream dataFile("data1.txt");
if(dataFile.fail())
{
cout<<"Unable to open file"<<endl;
exit(1);
}
double value = 0;
while(dataFile >> value)
cout<<"Read: "<< value << endl;
dataFile.close();
}
The output of this program is always Unable to open file.
please tell me why im not able to make files. im using a mac book pro (IED: coderunner 2) dont know if that makes a difrence
Depending on your IDE, the program might not be running in the location you think it is. Try putting your text file in different locations in the project hierarchy. It's likely that it's not in the right spot.
You need to verify the location of the file first. For example when running your program from a VS IDE you would not place the text file inside a Debug or Release folder. You would place your text file inside a folder from which your IDE launches an executable. If you ran a standalone executable then you would place your txt file in the same folder with the executable.
Since you are using your file for the input and not the output you could use the std::ifstream instead:
std::ifstream dataFile("data1.txt");
You don't have any code that creates a file.
To create a file:
1) Use std::ofstream
2) Use std::fstream with the correct mode.
Although not necessary, you should write data to the file to help create it.
The code in your post is reading from a file. You open a file, and the open may not be successful. You should have your program tell fstream the path to the file (to remove ambiguities).

file.open() on linux doesn't open my file, how to solve?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
file.open("./cp.txt");
if(file.good())
{
cout << "done!";
}
else
{
cout << "fail";
}
return 0;
}
why does my code can't cp.txt file? it is in project folder. i'm using gnu/linux.
as open i mean the program will open it in some text editor i.e:. leafpad
as open i mean the program will open it in some text editor i.e:. leafpad
That's not going to happen.
When you open an ifstream object it means the file is open for reading by that object, it does not mean a text editor is launched and displays the file!
Maybe what you want is:
system("leafpad cp.txt");
The system function runs another command, in this case it runs the command to launch leafpad with your file as an argument.
Note that the file will be searched for in the current working directory of your program, which is not the same as your "project directory". If you don't know what the current working directory is when your program gets run then you will need to provide an absolute path to the file, not a relative path like cp.txt
you have mentioned having a project folder. if you use an IDE,
it might change the current directory of the running executable.
try deleting the file and creating the file within your code eg:
ofstream ofile;
ofile.open("./cp.txt");
ofile.close();
if you get an output done! then search for the file.

I am having trouble opening a .txt file in my C++ program

I have put it under the Source Files folder. I have set my working directory to $(SolutionDir)$(Configuration)\ (im not sure if that is right, i saw it somewhere) could someone help me troubleshoot.
int main()
{
cout << "program running" <<endl;
pair<int, unsigned int> mypair;
ifstream myfile;
myfile.open("numbers.txt", ios::in);
if (!myfile.is_open()){
cerr << "can't open input file" << endl;
}
else
{
cout << "file opened" << endl;
}
getchar();
myfile.close();
}
...output is: can't open input file
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
There is such thing as current directory for an application. When you run an app from VS working directory would be current one.
If you specify file name with relative path (or no path at all) OS will try to find that file relative to that directory. Where executable file and especially source file(s) are located completely irrelevant. So solution could be either set working directory to where numbers.txt is located (or move nubers.txt there), or use relative path something like foobar/numbers.txt or even ../foobar/numbers.txt etc or use absolute path.
okay I managed to figure it out..with help :) I did what Paulo M said :
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
After I figured out where the file got sent I added a new file w/ some integers to that dir and made sure the working directory was the same.I'm not sure if i had to change it? (can files be sent elsewhere?).
I had tried to do this before but I was not able to move my txt file into this directory by copy paste..so gave up. But anyhow I just right clicked/new text document/ and edited the doc with some numbers. then changed my program to open this new document.
saving file directly to the directory also worked. :) :) :)
but i am curious why I couldn't just paste it there?

Code runs perfect in g++ but not in Xcode - Cannot find File

I have created a text file with content. It is located in the same folder as the cpp files. And I have confirmed several times that the file exists. When I run g++, compile and run it finds the file. When I run it in Xcode, it does not work. If fails to find the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Your file fails to open because XCode launches from the IDE in the default build location, which is actually a temporary directory off somewhere on your disk. If you want change the working directory at launch-time to something else (like the location where your files can be found):
Select the Product/Edit Scheme... menu option.
Select the Run schema in the left list.
At the bottom Options tab on the right pane should be a "Working Directory" option. Check the checkbox and set the custom working directory to someplace you know (your "/Users/yourname" home directory is a decent place that I use).
Make sure any "current directory" data files you need for your program execution from the IDE are in this directory.
And in case you didn't see it, this is also the place where you can configure command-line arguments (on another tab) of the same dialog.
Try to add files to the project in XCode or use the absolute path instead.

Opening a file in C++ outside of the working directory

I've got a program that is going to have several resource files that the user can put somewhere on the computer that isn't in the same folder as the executable. How do I get open those files?
I've found lots of answers saying that the reason things aren't working is that the file isn't in the working directory. I've tried providing fully qualified paths:
ifstream str;
str.open("/home/millere/foo.txt")
but that was unsuccessful. I know the path was correct (copy and paste). I can't find any documentation on it, but I assume it has to be possible. (vim ~/foo.txt from anywhere other than ~ works, for example).
Assuming you meant to use ifstream instead of iostream, your code is correct. ifstream can use a path to a file as well as the name of a file in the working directory.
No exceptions are thrown if the file does not exist, but the fail bit is set. You should check for this before trying to do anything with the stream.
std::ifstream input("/home/bob/stuff.txt");
if (!input) std::cerr << "Could not open the file!" << std::endl;
else
{
// ...
}
If you still cannot extract data from the file, the problem is somewhere else in your code.
I had the same issue and quickly noticed that, open when trying to get from a difference folder, had a different source directory (if using Cmake, the one that was specified by the cmake). You can find out, what the ouput/input source directory is by doing
system("ls")
or
system("dir")
on windows to show the content of the current ouput/input directory.