1

I have to read the status of the ethernet interface in embedded linux using C++ I attempt to open and read the file /sys/class/net/eth0/operstate, but i observed that fail() member function always returns true and the errno is permission denied. My application permission indicator is 755, i also tried set it 777, but the error is still there.

Below is my code:

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

    std::string word, filename;
    std::fstream ethfile;
    ethfile.open("/sys/class/net/eth0/operstate");

    if (ethfile.fail())
    {
        std::cout << "Error: " << strerror(errno) << '\n';
        return -1;
    }

    ethfile>>word;
    std::cout<<"word is"<<word<<std::endl;
    return 0;
}

EDIT: Additional info: this is yocto on beaglebone.

James Z
  • 12,209
  • 10
  • 24
  • 44
KJ L
  • 115
  • 2
  • 14

1 Answers1

2

You cannot open this particular file for writing no matter what your permissions are.

You need to use ifstream rather than fstream, or specify an open mode that does not include writing.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243