c++ implementation, need to get rid of scanf and create a in memory buffer instead