Opening /dev/serial/by-id/<SymbolicLink> fails - c++

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

Related

Ifstream is OK but Ofstream not working in Visual Studio (C++)(WIN11)

When I write a simple ofstream and ifstream instance ,ifstream is working but ofstream is not working. When I run this program ofstream is creating but I don't see output.txt in directory file.
I tried turning off COMODO Antivirus but the problem persists.
What is preventing me from seeing the output file?
here my First.cpp:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output("output.txt");
ifstream input("input.txt");
if (output.is_open()) {
cout << "ouput file is open" << endl;
output << "hi";
}
else {
cout << "output file is not open" << endl;
}
int number;
if (input.is_open()) {
cout << "input file is open" << endl;
input >> number;
cout << "Number:" << number << endl;
}
else {
cout << "input file is not open" << endl;
}
input.close();
output.close();
return 0;
}
here my input.txt:
21
12
22
23
32
when I run this program:
ouput file is open
input file is open
Number:21
I see that the issue was related to your Antivirus, but I'll write this anyways for others to read.
When dealing with IO its important to know what directory your executable will execute from. The working directory can be set in the project configuration. Right Click the project > Properties > Configuration Properties > Debugging > Working Directory. Configuration Window
The working directory is also the directory where all your application created files will be generated. For instance the example code generate output.txt at the project working directory.
std::ofstream output("output.txt");
if (!output.is_open())
return -1;
std::cout << "file is open" << std::endl;
output << "Hello there!";
output.flush();
output.close();
project directory screenshot

Check if using termux or normal distro in C++

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?

In Visual Studio 2019 can't read text file from release mode but works fine in debug (running within VS)

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.

How to read and write input file and output file

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.

XCode will not take input from a file

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.