0

I have a program that checks if apt, apt-get and dpkg are installed. But now I need to check if using a normal distro (like Mint, Ubuntu, etc.) or using termux to change the path, how can I do that?

I already tried this, but then it says the path doesn't exist (on a normal distro):

std::ifstream aptget("/usr/bin/apt-get");
std::ifstream dpkg("/usr/bin/dpkg");
std::ifstream termuxapt("/data/data/com.termux/files/usr/bin/apt");
std::ifstream termuxaptget("/data/data/com.termux/files/usr/bin/apt-get");
std::ifstream termuxdpkg("/data/data/com.termux/files/usr/bin/dpkg");
if (!apt.is_open()) {
    cout << "Path not found" << endl;
} else if(!aptget.is_open()) {
    cout << "Path not found" << endl;
} else if(!dpkg.is_open()) {
    cout << "Path not found" << endl;
} else if(!termuxapt.is_open()) {
    cout << "Path not found" << endl;
} else if(!termuxaptget.is_open()) {
    cout << "Path not found" << endl;
} else if(!termuxdpkg.is_open()) {
    cout << "Path not found" << endl;
} else {
    cout << "Path found" << endl;
}
  • Does this answer your question? [Fastest way to check if a file exist using standard C++/C++11/C?](https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c) – Shaked Eyal Jun 02 '21 at 17:16

1 Answers1

1

The problem with your code is that you are checking if the ifsteram is open before you open it. As you can look in this documentation: std::ifstream::is_open:

Returns whether the stream is currently associated to a file. Streams can be associated to files by a successful call to member open or directly on construction, and disassociated by calling close or on destruction.

So, what you can do is try and open each of the files. But it is risky.

There are better ways to check if files exist in C++. See more details in: Fastest way to check if a file exists using standard C++/C++11/C?

Shaked Eyal
  • 123
  • 1
  • 14