ERRNO 9 / Bad File Number after Hours accessing

Pardon, but this set of statements seems rather crude and redundant.

  One improvement would be to use a switch/case structure

  switch (channel)
    case 0:
      output_fd = ...;
    case 1:
      //value is not 0..7, dump an error and exit?

but that still has the redundancy in the open calls.

  if (channel < 0 || channel > 7)
    //value is not 0..7, dump an error and exit?
  // path needs to be a character buffer of sufficient length
  sprintf(path, "/sys/bus/iio/devices/iio:device0/in_voltage%1d_raw",
  output_fd = open(path, ...)

  Unfortunately I have no help for the "bad file number" condition. My
naive thought was that the runtime keeps incrementing the file number of
the fd on each open, and isn't recycling around to reuse file numbers.
However, that doesn't match the description for POSIX compliant open()

  Might it be possible to only open the 8 channels at the start of the
program, outside of the loop, and use something like

  lseek(channel_fd, 0, SEEK_SET);
  //and then read the channel_fd

{I've not tried seeking on the sysfs entries}