The first thing you should ask yourself is: why do I want to change the current directory? Is it just a program for you, to make it more comfortable to work from the terminal? Is it a simple attempt, or programming game to see what can be done? Or is it part of a larger project, and one of the things you should do is change the current directory for any other reason?
Bash
If it is to be able to work more comfortably from a terminal bash
, that already exists:
The pushd
and popd
commands:
$ pushd nuevo_directorio # Ahora estás en nuevo_directorio
$ pushd otro_directorio # Ahora estás en otro_directorio
$ popd # Ahora estás en nuevo_directorio
$ popd # Ahora estás en el directorio original.
Each call to pushd
saves the new directory in a stack, thus you accumulate a call history. Each call to popd
, returns to the previous directory, deleting the last one (the top), from the call history.
If your history is going to have a maximum size of one (ie, go back and forth, and that's it), cd
already provides a way to go back to the previous directory:
$ cd nuevo_directorio # Cambia el directorio actual
$ cd - # Vuelve al directorio donde estabas
If you're on Windows, no idea.
Experimenting with C ++
If it is simply to see what you can do from C ++, you can use system
.
You simply call bash
commands (or MS-DOS
if you are in Windows, or the terminal language associated with your operating system):
#include <cstdlib>
// ...
system("pushd nuevo_directorio");
system("popd");
Or with cd
, or whatever you want.
If it is cumbersome to work with system
( system
receive low level chains, and maybe you want to work with std::string
s), you have two other ways. With Qt
:
QDir::setCurrent("nuevo_directorio");
or with boost
:
#include <boost/filesystem.hpp>
boost::filesystem::current_path("nuevo_directorio");
Obviously, with these two solutions, in addition to being cross-platform (be it Windows, Linux, Android, etc.), you can work with high-level chains, carry your history in a std::stack
or a std::vector
, or their corresponding types of Qt
, etc.
Additionally, you can check that the directory exists (or file, in case you want to open a file in this directory), passing the relative paths to absolute with boost::filesystem::canonical
, which throws an exception if the given route does not exist or There are some kind of permission problems:
try {
ruta_file = boost::filesystem::canonical(path_dir);
} catch (const boost::filesystem::filesystem_error& err) {
std::cerr << err.what() << std::endl; // Mostrar el error
}
// path_dir es una ruta absoluta a partir de aquí.
Real project
Perhaps you are a victim of the XY problem , which consists in finding a solution to a problem different from the real one. Your real problem may be that you can not open a file because of an incorrect route or lack of permissions, but instead of solving the problem of opening a file, you try to solve it by changing the directory.
In any case, I do not defend, as a solution, changing the current directory of the application. That can give you real headaches in the long run. What if an exception is thrown after changing the directory and before returning to the original directory? How do you make sure you're always in the directory you want? What if you have an error in your directory history and move to unforeseen directories?
To do this, use the principle KISS : Keep it simple stupid Of course, I'm not insulting you It is a design principle. The simplest solution, if your real problem is to open files, is, of course, to make sure that the routes are correct.
If you have problems opening the file, it may be because:
-
the user is not indicating it well. Problem of the user of your application, and not yours as a programmer. The user must know from where the application is running, for something it is he who launches it. At the most you could inform the user that there is a problem to open the file.
-
or because you have permission problems. Maybe the file or files that you want to open are in a directory without execution permission for the application, for example. In any case, again, it is the user's problem, given that it is an environmental problem.
-
If the application has sufficient permissions and the user gives you the relative path of the directory correctly, perhaps you are not saving the string well. Maybe you saved a line break, or maybe the file name contains spaces and you're just guardnaod part of the string as a path.
If you still insist, the any of the solutions given above in C ++ given above should be worth it.