I am trying to read serial data coming from arduino, but when I run my program it only reads all the data in the buffer, the data that was actually send before the program started. And then i terminates with the following error:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): read: End of file
Aborted (core dumped)

I also do not even want the data from before the program was executed. Here is my c++ code:

So I have figured out that after running Arduino IDE as root, to allow serial communication, my programs stops working and I get the EOF error. Even after Arduino IDE has been closed it does not work, restarting my pc allows it to work again.