Path to file used as environment variable - c++

My C++ code for outputting content to a file is:
fstream myfile;
myfile.open("file.txt", ios::in);
if(!myfile.is_open()) cout << "error";
I want my code to function in the following way:
If file.txt is located in the same directory as the built executable or the cpp file, then my code should open the file.
Currently this is not the case. When I checked the location of the current working directory with getcwd it showed /User/myusername instead of /User/myusername/Desktop where all the files actually are located.

Related

Can' open File in C++ under MacOs

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.

Unable to Open .txt File

std::ifstream infile;
infile.open("example.txt");
if (!infile.is_open()) {
std::cout << "can't open" << std::endl;
}
I read this: unable to open file stream c++ and have tried using the full path infile.open("~/projects/example.txt");
The text file is in the same folder where the .cpp / .hpp files reside.
What else could be the problem?
try "realpath ~/projects/example.txt", which will show you the real path of your file, and then use this real path in your cpp
I once had the same problem on Windows. The problem was that the actual file name was "example.txt.txt", but Window Explorer was configured (by default) to hide file extensions.
A simple solution is to use c++17 filesystem library. For example, current_path will return what its name promises. Or write a simple function that will list the current path contents (see the example on cppreference, https://en.cppreference.com/w/cpp/experimental/fs/directory_iterator ) . Or create a file of some similar name, e.g. __example__.txt and see where it was created and under which name it is reported by your filesystem manager.
If this is Linux, try running your program under strace (strace myprog myoptions).

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.

Reading from folder in working directory C++

I want to read from a text file which is saved in a folder in my working directory. But unless the text file is in the working directory, it wont open.
Im using a line like this one. what should it be changed to?
ifstream myfile ("./folder/example.txt");
If you are on Windows, use this format to open the file.
ifstream myfile ("C:\\Book\\file.txt") ;
Supposing your file is in Book folder of C Drive.
First write the Directory and then the subsequent folders with double slashes in between till you reach the text file.
Though it is better if you put in your project directory itself, using this.
ifstream myfile ("file.txt") ;
You can access the directory your program is, by simply writing
ifstream myfile ("example.txt");
If example.txt is in the same directory as your program.
Note that this may not work if you can't save in this directory.