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;
}
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?
Related
I am currently trying to read in a text file for use in a project but when running in release form (running the .exe file from a command window) the file doesn't read in. But yet in debug mode it works fine. I have tried having the files in the same directory as the .exe file but it doesn't seem to find it and I don't know why?
The code:
std::cout << "Acquiring data file..." << endl;
std::cout << "Reading data file..." << endl;
ifstream tempData;
tempData.open("temp_lincolnshire_short.txt");
if (tempData.is_open()) {
std::cout << tempData.rdbuf() << endl;
}
else {
cout << "Reading failed..." << endl;
}
tempData.close();
std::cout << "Data file closed." << endl;
What I ended up using was a relative path as identified on https://stackoverflow.com/a/35910234/3795116 which ended up being:
myFile.open("../Release/frequency.dat", ios::in);
*changing myFile to whatever your variable is.
I try to open a symlink with a ifstream (C++). I run the program under OpenSuse 12.2.
Here is the sampleCode:
ifstream bufferLink("/dev/serial/by-id/usb-©_Datalogic_2002_Datalogic_Bar_Code_Scanner-if00");
if (bufferLink.is_open()) {
cout << "OK" << endl;
} else {
cout << "Error" << endl;
}
Sadly, I cannot open the device-link.
I can open (for example) the device-link "/dev/input/by-id/usb-No_brand_KVM-event-kbd", no problem.
Does anyone has an idea why I cannot open the link under /dev/serial/by-id
Okay guys, I've tried everything I can think of. I'm passing in a file name into this function. A little context: hash_table is an already initialized and filled vector with key pairs, and the 'value' part of the pair is a Linked List that has the field "bucket_size". When I use cout to check if these fields are actually being accessed, they are; even the debugger lists them as being filed into the output stream. I have flush() and close() in there, but it doesn't write anything to the file. Returns true, indicating no errors in the stream. Anyone have nay ideas?
string line;
std::ofstream ofs;
if(ofs.is_open())
ofs.close();
ofs.open(filename);
if (ofs.is_open())
{
cout << "File Opened" << endl;
for (double i = 0; i < hash_table.capacity(); ++i)
{
ofs << "Bucket Number " << i;
if (hash_table[i].value != NULL)
ofs << " Bucket Size: " << hash_table[i].value->bucket_size << endl;
else
ofs << " Bucket Size: 0" << endl;
ofs.flush();
}
cout << "closing file stream" << endl;
ofs.flush();
ofs.close();
if (ofs.good())
return true;
else
return false;
}
else
{
cout << "File not opened" << endl;
return false;
}
}
You're almost certainly examining the wrong file. Remember that relative paths are relative to the working directory of the process, which is not necessarily the same as where the executable lives on disk.
I compiled and ran it by the console and now it works, with no edits made. It seems my IDE doesn't like something in the code. Regardless, thank you everyone for the response.s
I am trying to run the following program:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double first=1.49, second=2.59, third=3.69, fourth=4.79;
inFile.open("prices.txt");
char response;
if(!inFile.fail())
{
cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
return -1;
}
}
outFile.open("prices.txt");
if (inFile.fail())
{
cout << "\n The file does not exist and can not be opened" << endl;
cout << "The file has been successfully opened for output." << endl;
outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
outFile.close();
exit(1);
cout << "The file has been successfully opened for output. " << endl;
outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
outFile.close();
return 0;
}
}
Yet this program will not write the values to the prices.txt file. If you run the program once it says the file does not exist. Running it a second time says the file is already there and if you want to overwrite it. The thing is searching my Mac I cannot find this file anywhere.
Any ideas what I am doing wrong with running it in Xcode? A friend runs the exact same code in Visual Studio 2008 and it works. Any help is appreciated.
You need to set the working directory for the executable since you are assuming that your data files are in the current working directory. In Xcode 3.x you set this in the first tab of Get Info for the executable. In Xcode 4.x it has been moved, but the principle is the same.
Alternatively you can change your data file paths (e.g. make them absolute) so that you do not make assumptions about the current working directory.
You may not have permission to write into the directory that you are trying to save the file too.
Also, there is an error in your program and I am sure if it is there for debugging reasons. You have
outFile.close();
exit(1);
But then shortly there after you try to write to the file, then close it again.
For some reason, Xcode will not take input from a file, while Visual C++ will.
When I run this program in xcode, the variables numberRows and numberCols stay 0 (they are initialized to 0 in the main function).
When I run it in Visual C++ they become 30 and 30 (the top line of maze.txt is "30 30" without the quotes).
Any ideas why this is happening?
void readIn(int &numberRows, int &numberCols, char maze[][100]){
ifstream inData;
inData.open("maze.txt");
if (!inData.is_open()) {
cout << "Could not open file. Aborting...";
return;
}
inData >> numberRows >> numberCols;
cout << numberRows << numberCols;
inData.close();
return;
}
There is something else wrong.
Unfortunately it is hard to tell.
Try flushing the output to make sure you get the error message:
void readIn(int &numberRows, int &numberCols, char maze[][100])
{
ifstream inData("maze.txt");
if (!inData) // Check for all errors.
{
cerr << "Could not open file. Aborting..." << std::endl;
}
else
{
// Check that you got here.
cerr << "File open correctly:" << std::endl;
// inData >> numberRows >> numberCols;
// cout << numberRows << numberCols;
std::string word;
while(inData >> word)
{
std::cout << "GOT:(" << word << ")\n";
}
if (!inData) // Check for all errors.
{
cerr << "Something went wrong" << std::endl;
}
}
}
interesting, so I followed the following suggestion from this post http://forums.macrumors.com/showthread.php?t=796818:
Under Xcode 3.2 when creating a new
project based on stdc++ project
template the target build settings for
Debug configuration adds preprocessor
macros which are incompatible with
gcc-4.2:
_GLIBCXX_DEBUG=1
_GLIBXX_DEBUG_PEDANTIC=1
Destroy them if you want Debug/gcc-4.2
to execute correctly.