Cannot run the code after I activate the plugin

you can not, and should not be able to write to files directly. You need to use the wordpress filesystem api https://codex.wordpress.org/Filesystem_API which either requires a pre configuration on the site or user manual control.

In addition it is just wrong to automatically write anything into wp-config.php as any mistake will bring down the site with no way to recover except for recovering from backup which is rarely an easy process.