Major errors reading multiple AIN from the Beaglebone using C++.

Hi friends,

I’m trying to read the voltage inputs from two analog IR distance sensors plugged into AIN0 and AIN1 using C++ code.

I used Derek Molloy’s code which can successfully read voltage values from one Analog pin. However, whenever I modify the code and try to read more than one analog pin, I receive this error:

terminate called after throwing an instance of ‘std::ios_base::failure’
what(): basic_filebuf::underflow error reading the file

I’m not really sure what this error is or why I’m getting it. Please let me know if you have knowledge on this matter:

Code is as follows:

using namespace std;

#define LDR_PATH “/sys/bus/iio/devices/iio:device0/in_voltage”

int readAnalog(int number){

stringstream ss;

ss << LDR_PATH << number << “_raw”;

fstream fs;, fstream::in);

fs >> number;


return number;


int main(int argc, char* argv[]){

cout << “Starting the IR distance sensor program:” << endl;

while(1) {

int value0 = readAnalog(0);
int value1 = readAnalog(1);
float V0 = ((float)value0 / 4096) * 1.8;
float V1 = ((float)value1 / 4096) * 1.8;

cout << "Sensor0 is: " << V0 << " Sensor1 is: " << V1 << ‘\r’ << flush;



return 0;

I know this is some sort of access error, and there is a workaround for the python libraries (see: ). But I am unsure as to how I can use this information in the context of C++ code.